httpClient发送https请求代码
来源:互联网 发布:orancle in优化 编辑:程序博客网 时间:2024/05/29 18:58
package com.lvmama.dest.dianping;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.util.EntityUtils;public class TestHuiping {static String url = "https://***/HC_API/api.php";public static void main(String[] args) {test();}public static void test(){HttpPost httpPost = null; try { String param = "{\"HotelID\":\"637ac99596e0d635\"***\"Method\":\"getHotel\"}"; httpPost = new HttpPost(url); HttpEntity requestEntity = new StringEntity(param); httpPost.setEntity(requestEntity); HttpClient httpClient = createHttpsClient(10000,10000); HttpResponse resp = httpClient.execute(httpPost); HttpEntity entity = resp.getEntity();String responseStr = EntityUtils.toString(entity, "UTF-8");if (entity.getContentType() == null) {responseStr = new String(responseStr.getBytes("iso-8859-1"), "UTF-8");}EntityUtils.consume(entity); System.out.println(responseStr); //String str = HttpsUtil.requestPostHttpsData(url, param);// System.out.println(str); } catch (Exception e) { e.printStackTrace(); }}public static HttpClient createHttpsClient(int connectionTimeout,int soTimeout) { try { HttpClient httpClient = new DefaultHttpClient(); //创建默认的httpClient实例 HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, soTimeout); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{new TrustAnyTrustManager()}, null); SSLSocketFactory socketFactory = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory)); return httpClient; } catch (Exception e) { e.printStackTrace(); } return null; }private static class TrustAnyTrustManager implements X509TrustManager {public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[] {};}}}
</pre><pre code_snippet_id="1818761" snippet_file_name="blog_20160809_3_8076164" name="code" class="java">
javax.net.ssl.SSLException: hostname in certificate didn't match
这个错误通过SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER避免
0 0
- httpClient发送https请求代码
- HTTPClient 发送HTTPS请求
- httpClient发送https请求
- HttpClient 发送 HTTP、HTTPS 请求
- HttpClient 之 发送Https请求
- 使用HttpClient发送https请求GET方式
- HttpClient向HTTPS地址发送POST请求
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- 使用HttpClient向HTTPS地址发送POST请求4.1.2
- 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- HttpClient学习笔记 --发送Http和Https请求
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- 使用Apache提供的HttpClient发送https请求
- httpclient发送https的get请求忽略证书
- HttpClient 4.3 发送HTTPS
- HttpClient请求Https协议
- android:theme中各个颜色的对应位置
- Android之判断网络类型
- RecyclerView和ListView使用对比分析
- HTML5-表单
- ReentrantLock和synchronized比较
- httpClient发送https请求代码
- Hadoop2.7.0学习——移除节点
- 51nod oj 1264 线段相交【数学几何】
- 最大子矩阵(动态规划)
- LeetCode Swap Nodes in Pairs
- netty中LengthFieldBasedFrameDecoder的使用
- 使用IntelliJ IDEA开发SpringMVC网站(五)博客文章管理
- 表白吧!程序猿!
- Boost Log 1