0

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

1
  • This code works fine in my machine. Please show the full stacktrace. Commented Jun 11, 2021 at 17:47

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.