java.net.SocketTimeoutException: Connect timed out

An http request to any site throws a SocketTimeoutException. What could this be related to?

public class Main {

    public static void main(String[] args) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL("https://coderlessons.com/tutorials/java-tekhnologii/uznaite-jsoup/jsoup-kratkoe-rukovodstvo").openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(2000);
            connection.setReadTimeout(2000);
            connection.connect();

            if(HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String str = reader.readLine();
                while (str != null) {
                    System.out.println(str);
                    str = reader.readLine();
                }

                reader.close();
            }
            else{
                System.out.println("Error " + connection.getResponseCode());
            }

            connection.disconnect();
        }
        catch (MalformedURLException ex){
            ex.printStackTrace();
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    }
}

I tried to connect to the server via URLConnection, but the Connectexception exception is caught

Upvotes: 0

Views: 3199

Answers (2)

nuwa
nuwa

Reputation: 1

I think the problem is 'https'. you can't send request to https URL without certificate. please try http URL or use SSL certificate.

Upvotes: 0

TheAngryPirate
TheAngryPirate

Reputation: 1

Could be because of the cookie, check the header of the URL for the same.

If it's static just copy and paste in your code,

if dynamic then collect is using response and then pass.

Try might work.

Upvotes: 0

Related Questions