gpt4 book ai didi

java - 尝试下载文件时,Apache HttpClient 在握手异常期间抛出远程主机关闭连接

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

我有一个 url,其中可以通过 http 下载文件。如果我用 curl 点击那个 url,文件下载没有问题。但是,如果我尝试使用 Apache HttpClient 编写代码,它会出现异常。这是代码...

HttpClient httpClient = new HttpClient()
HttpMethod method = new GetMethod("https://www2.mycompany.com/internet/cats/productfeed.nsf/xmlproductfeed?openview")
def responseCode = httpClient.executeMethod(method)

异常(exception)是

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:882)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:654)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:100)
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:828)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2116)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)

可以是url中的https还是www2?

最佳答案

您正在通过 HTTPS 调用 URL,这是一个 TLS 连接。您需要将客户端设置为使用 SSL/TLS。

这里是一个例子,重写为HttpGet,但这应该很容易:

// Trust own CA and all self-signed certs
SSLContext sslcontext = createEasySSLContext();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

String userCredentials = username + ":" + password;
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));

CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(credentialsProvider);

HttpPost httpPost = new HttpPost(path);

关于java - 尝试下载文件时,Apache HttpClient 在握手异常期间抛出远程主机关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646863/

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