我有一个网址,可以通过http下载文件。如果我用卷曲命中该网址,文件下载没有问题。但是,如果我尝试使用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)
例外是
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)
可能是网址中的https或www2吗?
答案 0 :(得分:3)
您正在通过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);