gpt4 book ai didi

java.net.SocketException : Connection reset for inputstream 异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:05 25 4
gpt4 key购买 nike

我正在尝试从 https URL 下载文件。我可以使用以下代码从我的家用电脑下载文件。但是,我无法从公司机器上下载它。所需的 cacerts 已正确导入 java。我还可以从浏览器下载文件。我查看了其他线程的数量,但无法找到解决此问题的合适方法。这是我的Java代码

 try
{
url = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
inputStream = httpURLConnection.getInputStream();
else
inputStream = httpURLConnection.getErrorStream();
}
catch (Exception ioe)
{
ioe.printStackTrace();
}

这是我遇到的异常。我总是在 inputstream 处遇到异常。请记住,此代码在我的家用 PC 上 100% 有效。

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)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:706)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)

任何线索都会有很大的帮助。

最佳答案

对我来说很好:

    System.setProperty("http.proxyHost", "host");
System.setProperty("http.proxyPort", "port");
try {
URL u = new URL("some url");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
InputStream is = null;
if (huc.getResponseCode() == HttpURLConnection.HTTP_OK)
is = huc.getInputStream();
else
is = huc.getErrorStream();
} catch (Exception e) {
e.printStackTrace();
}

关于java.net.SocketException : Connection reset for inputstream 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57406357/

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