У меня проблемы с чтением API с HTTPClient 4.1.x, HttpPost, SSL-сертификатом.Когда я пытаюсь прочитать API с протоколом "http".Все нормально.Но когда я пытаюсь прочитать API с протоколом "https".Это не работает.Выдает исключение «org.apache.http.conn.HttpHostConnectException» с подключением: «Соединение с https://example.com отказано в нуле»
Это мой код:
public class CallAPI {
public static void main(String[] args) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("a", "");
params.put("b", new File("D://File.txt"));
Map<Integer, String> responseDataMap = new HashMap<Integer, String>();
String url = "https://example.com";
responseDataMap = callAPIwithFormDataSSL(url, params);
}
private static Map<Integer, String> callAPIwithFormDataSSL(String url, Map<String, Object> params) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sslsf = new SSLSocketFactory(
acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpPost httppost = new HttpPost(url);
int port;
if (httppost.getURI().getPort() == -1) {
port = 80;
} else {
port = httppost.getURI().getPort();
}
Scheme scheme = new Scheme("https", port , sslsf);
httpclient.getConnectionManager().getSchemeRegistry().register(scheme);
HttpResponse response = null;
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String key = null;
Object object = null;
for (Map.Entry<String, Object> entry : params.entrySet()) {
key = entry.getKey();
object = entry.getValue();
if (object instanceof String) {
entity.addPart(key, new StringBody(object.toString()));
} else if (object instanceof File) {
entity.addPart(key, new FileBody((File) object));
} else {
entity.addPart(key, null);
}
}
httppost.setEntity(entity);
response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
String resEntity = EntityUtils.toString(response.getEntity());
Map<Integer, String> responseDataMap = new HashMap<Integer, String>();
responseDataMap.put(statusCode, resEntity);
return responseDataMap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Исключение:
org.apache.http.conn.HttpHostConnectException: Connection to https://example.com refused null
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:575)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at xxx.CallAPI.callAPIwithFormDataSSL(CallAPI.java:75)
at xxx.CallAPI.main(CallAPI.java:35)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:375)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
... 9 more