Https工具类
来源:互联网 发布:淘宝要好评 编辑:程序博客网 时间:2024/05/16 18:47
import java.io.IOException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.HashMap;import java.util.Map;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import net.sf.json.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpDelete;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpPut;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.util.EntityUtils;/** * HttpClient Utils * * @author Lynch * */@SuppressWarnings("all")public class HttpsUtils {public static String Method_GET = "GET";public static String Method_POST = "POST";public static String Method_PUT = "PUT";public static String Method_DELETE = "DELETE";/** * Send SSL Request * * @param reqURL * @param token * @param str * @return */@SuppressWarnings("finally")public static String sendSSLRequest(String reqURL, String token, String body, String method) {String responseContent = null;HttpClient httpClient = new DefaultHttpClient();// HttpClient httpClient = HttpClients.createDefault();X509TrustManager xtm = 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;}};try {SSLContext ctx = SSLContext.getInstance("TLS");ctx.init(null, new TrustManager[] { xtm }, null);SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));HttpResponse response = null;if (method.equals(Method_POST)) {HttpPost httpPost = new HttpPost(reqURL);httpPost.setEntity(new StringEntity(body, "UTF-8"));if (token != null) {httpPost.setHeader("Authorization", "Bearer " + token);}response = httpClient.execute(httpPost);} else if (method.equals(Method_PUT)) {HttpPut httpPut = new HttpPut(reqURL);httpPut.setEntity(new StringEntity(body, "UTF-8"));if (token != null) {httpPut.setHeader("Authorization", "Bearer " + token);}response = httpClient.execute(httpPut);} else if (method.equals(Method_GET)) {HttpGet httpGet = new HttpGet(reqURL);if (token != null) {httpGet.setHeader("Authorization", "Bearer " + token);}response = httpClient.execute(httpGet);} else if (method.equals(Method_DELETE)) {HttpDelete httpDelete = new HttpDelete(reqURL);if (token != null) {httpDelete.setHeader("Authorization", "Bearer " + token);}response = httpClient.execute(httpDelete);}HttpEntity entity = response.getEntity();if (null != entity) {responseContent = EntityUtils.toString(entity, "UTF-8");EntityUtils.consume(entity);}} catch (Throwable e) {e.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}return responseContent;}public static void main(String[] args){String username = "3";String password = "123456";JSONObject json = new JSONObject();json.put("username", username);json.put("password", password);System.out.print(sendSSLRequest("https://a1.easemob.com/xiaoliapp/uni/users","",json.toString(),Method_POST));}}
http://hc.apache.org/httpclient-3.x/sslguide.html
0 0
- Https联网工具类
- Https工具类
- https封装工具类
- https最新工具类
- HttpClient HTTPS 工具类
- http https请求工具类
- Retrofit2 配置Https 工具类
- Java Https请求工具类
- 通用http、https访问工具类
- 安卓HTTPS操作工具类
- iOS - 加密工具类和HTTPS
- HttpClient实现的https工具类
- 调用https接口的工具类
- Https处理工具
- post/get请求(http,https)工具类
- jsoup请求http或https返回json字符串工具类
- JAVA发送HTTPS的POST请求工具类
- http https信任任何证书的工具类
- C语言基础总结-符号的输入输出(常用)
- navigationBar 和 navigationItem 设置 titleView 时的不同
- 时间复杂度和空间复杂度(1)
- Java 文件I/O常用API 以及上传下载路径问题汇总
- 机器学习系列(5)_从白富美相亲看特征选择与预处理(上)
- Https工具类
- xss 测试
- 学习使用MEAN开发RESTful WEB api,实现数据的CRUD
- 17. Letter Combinations of a Phone Number
- Android网络开发框架Retrofit(四:扩展篇,Retrofit+RxJava)
- Hessian
- 秒杀多线程第六篇 经典线程同步 事件Event
- Android起航系列第二章:为app添加action bar
- SQLSERVER 分布式事务开放