博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java Https工具类
阅读量:5731 次
发布时间:2019-06-18

本文共 4146 字,大约阅读时间需要 13 分钟。

import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;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.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/** * https工具类 *  * @author remainsu * 2019-05-05 */public class HttpsUtil {        /**     * post方式访问     * @param url 路径     * @param map 参数     * @return     */    public String httpsPost(String url, Map
map) { String charset = "UTF-8"; HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { httpClient = new SSLClient(); httpPost = new HttpPost(url); //设置参数 List
list = new ArrayList
(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry
elem = (Entry
) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception ex) { ex.printStackTrace(); } return result; } /** * get方式访问(如果有参数直接 ?xx&yy&zz 的方式即可) * @param url * @return */ public String httpsGet(String url) { String charset = "UTF-8"; HttpClient httpClient = null; HttpGet httpGet= null; String result = null; try { httpClient = new SSLClient(); httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,charset); } } } catch (Exception e) { e.printStackTrace(); } return result; }}class SSLClient extends DefaultHttpClient { //用于进行Https请求的HttpClient public SSLClient() throws Exception { super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() {return null; } }; ctx.init(null, new TrustManager[]{tm}, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); }}

转载于:https://www.cnblogs.com/remainsu/p/java-https-gong-ju-lei.html

你可能感兴趣的文章
服务的使用
查看>>
Oracle 用户与模式
查看>>
网站开发流程以及HTML5简介(八)
查看>>
MairDB 初始数据库与表 (二)
查看>>
RabbitMQ】三种Exchange模式——订阅、路由、通配符模式
查看>>
连接数据库——java
查看>>
拥在怀里
查看>>
chm文件打开,有目录无内容
查看>>
whereis、find、which、locate的区别
查看>>
TRUNK
查看>>
一点不懂到小白的linux系统运维经历分享
查看>>
MDT 2013 从入门到精通之软件自动化部署设置
查看>>
桌面支持--打不开网页上的pdf附件解决办法(ie-tools-compatibility)
查看>>
桌面支持--outlook取消收件规则1
查看>>
nagios监控windows 改了NSclient++默认端口 注意事项
查看>>
干货 | JAVA代码引起的NATIVE野指针问题(上)
查看>>
POI getDataFormat() 格式对照
查看>>
Project build error: Non-resolvable import POM
查看>>
Python 中的进程、线程、协程、同步、异步、回调
查看>>
swoft速学~redis引入
查看>>