Java Https请求工具类
来源:互联网 发布:页游多开软件 编辑:程序博客网 时间:2024/06/06 01:10
由于微信API接口建议使用Https请求方式 而且过不久就废弃http请求方式了 所以提供以下Https工具类
public class SSLClient extends DefaultHttpClient{ public SSLClient() throws Exception{ super(); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override 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请求工具类
public class HttpClientUtil { /** * 发送post请求 * @param url * @param map * @param charset * @return */ public static String doPost(String url,Map<String,String> map,String charset){ HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try{ httpClient = new SSLClient(); httpPost = new HttpPost(url); //设置参数 List<NameValuePair> list = new ArrayList<NameValuePair>(); Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Entry<String,String> elem = (Entry<String, String>) 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请求 * @param url 链接地址 * @param charset 字符编码,若为null则默认utf-8 * @return */ public static String doGet(String url,String charset){ if(null == charset){ 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; }}
本工具类根据http://blog.csdn.net/rongyongfeikai2/article/details/41659353/进行了小改
阅读全文
0 0
- Java Https请求工具类
- http https请求工具类
- JAVA发送HTTPS的POST请求工具类
- Java https请求 HttpsURLConnection
- java 实现https请求
- java 实现https请求
- java Https的请求
- java 发送 https请求
- java 实现https请求
- Java https请求 HttpsURLConnection
- JAVA请求HTTPS协议
- java访问https请求
- java发送https请求
- JAVA请求HTTPS协议
- java 实现https请求
- java模拟https请求
- java模拟https请求
- java 实现https请求
- 机器学习实战-13利用PCA来简化数据
- 代码中设置EditView的光标
- [leetcode]#35. Search Insert Position
- 在Android中使用SQLite的附加数据库(Attach DB)
- 树莓派挂载U盘
- Java Https请求工具类
- Numpy—np.random.seed()函数的应用
- android APP如何上线以及所做的哪些准备
- 小蓝单车解散迷局:高管分歧、退押金走后门、HR变卖家具
- Java中,&&与&,||与|的区别
- Servlet 中 session 的创建、销毁及监听 原创 2016年03月23日 18:09:33 标签:session /session超时 /在线人数统计 4916 1、session 和 c
- android的MVP架构
- filedownloader使用
- Chrome插件baiduexporter+aria2百度网盘下载