我试图在java中使用CloseableHttpClient和HttpPost来使用https休息Web服务,但是没有这样做。我认为它与我的请求有关,也与ssl和协议TLS有关,但我真的不太了解它。我可以向邮递员提出请求就好了。
这是我的代码:
public class RestHttpsPiloto {
//private static final String POST_URL = "https://www.google.com";
private static final String POST_URL = "https://<URL>";
public static void httpsPilotoMail() throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException{
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//CloseableHttpClient httpClient=createAcceptSelfSignedCertificateClient();
HttpPost httpPost = new HttpPost(POST_URL);
//HttpGet httpGet = new HttpGet(POST_URL);
String payload = "data={" +
"\"correo\": \"email\", " +
"\"rut\": \"xxxxxxx-x\", " +
"\"nombre\": \"nombre\", " +
"\"idHtml\": \"21\", " +
"\"token\": \"JAHNSMsamJ\"" +
"}";
StringEntity entity = new StringEntity(payload);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
//CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println("POST Response Status:: "
+ httpResponse.getStatusLine().getStatusCode());
httpClient.close();
}
private static CloseableHttpClient createAcceptSelfSignedCertificateClient()
throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
// use the TrustSelfSignedStrategy to allow Self Signed Certificates
SSLContext sslContext = SSLContextBuilder
.create()
.loadTrustMaterial(new TrustSelfSignedStrategy())
.build();
/*SSLContext sslContext = SSLContexts.custom().useSSL().build();
sslContext.init(null, new X509TrustManager[]{new HttpsTrustManager()}, new SecureRandom());
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);*/
// create an SSL Socket Factory to use the SSLContext with the trust self signed certificate strategy
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext);
//SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2"}, null, null);
//System.setProperty("https.protocols", "SSLv3");
// finally create the HttpClient using HttpClient factory methods and assign the ssl socket factory
return HttpClients
.custom()
.setSSLSocketFactory(connectionFactory)
.build();
}
public static void main(String[] args) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException{
httpsPilotoMail();
}
}
我必须补充一点,closableHttpClient的创建适用于传统方法和createAcceptSelfSignedCertificateClient()方法。此外,使用此代码,我可以向谷歌发送https get请求。我还想补充一点,我为了隐私目的更改了网址...所以我真的希望在我的代码中找到一些东西,因为我知道网址是有效的,我无法发布它。
这是我的stacktrace:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:422)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:460)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:863)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at com.vtr.pat.controller.RestHttpsPiloto.httpsPilotoMail(RestHttpsPiloto.java:56)
at com.vtr.pat.controller.RestHttpsPiloto.main(RestHttpsPiloto.java:94)
非常感谢先进。