gpt4 book ai didi

在页面 GET 上使用 HttpURLConnection 时出现 java.net.SocketException : Connection reset,

转载 作者:行者123 更新时间:2023-12-02 10:18:13 24 4
gpt4 key购买 nike

使用 Java (HttpURLConnection) 以编程方式获取页面会出现错误 java.net.SocketException:连接重置。可与 Python 和 Postman 配合使用。

尝试过其他页面,如 gmail 登录页面、电子银行登录页面等,它可以工作,但仅适用于一个网站(我需要它工作的网站)不行。使用Python脚本成功,Postman也可以工作,但Java不行

String url="https://********************/Login";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestMethod("GET");

int responseCode = con.getResponseCode();

预计不会抛出异常。实际结果是:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
...

有效的Python脚本:

import urllib.request  as urllib2 
content = urllib2.urlopen('https://******************/Login').read()
print(content)

最佳答案

尝试 HttpsURLConnection:

String url = "https://********************/Login";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735) 中的代码

enter image description here

也许代理是问题的原因。

关于在页面 GET 上使用 HttpURLConnection 时出现 java.net.SocketException : Connection reset,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54532763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com