簡體   English   中英

java.net.SocketTimeoutException:連接超時

[英]java.net.SocketTimeoutException: Connect timed out

對任何站點的 http 請求都會引發 SocketTimeoutException。 這可能與什么有關?

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();
        }
    }
}

我嘗試通過 URLConnection 連接到服務器,但捕獲到 Connectexception 異常

可能是因為 cookie,檢查 URL 的 header 是否相同。

如果是 static 只需復制並粘貼到您的代碼中,

如果動態然后收集使用響應然后通過。

嘗試可能會奏效。

我認為問題是“https”。 沒有證書,您無法向 https URL 發送請求。 請嘗試 http URL 或使用 SSL 證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM