Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**
     * Given a string url, connects and returns response code
     *
     * @param urlString       string to fetch
     * @param readTimeOutMs       read time out
     * @param connectionTimeOutMs       connection time out
     * @param urlRedirect       should use urlRedirect
     * @param useCaches       should use cache
     * @return httpResponseCode http response code
     * @throws IOException
     */

    public static int checkUrlWithOptions(String urlString, int readTimeOutMs, int connectionTimeOutMs,
            boolean urlRedirect, boolean useCaches) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setReadTimeout(readTimeOutMs /* milliseconds */);
        connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */);
        connection.setRequestMethod("GET");
        connection.setInstanceFollowRedirects(urlRedirect);
        connection.setUseCaches(useCaches);
        // Starts the query
        connection.connect();
        int responseCode = connection.getResponseCode();
        connection.disconnect();
        return responseCode;
    }
}