ClientUtil.java


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.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; public class ClientUtil {
private String charset = "utf-8";
public String doPost(String url, Map<String,String> map){
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;
}
}

SSLClient.java


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 javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; //用于进行Https请求的HttpClient
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));
}
}

最新文章

  1. javascript平时例子⑧(大屏轮播)
  2. SQL SERVER各种用法总结
  3. Android自动化测试工具——monkey简介及入门
  4. android自定义view属性
  5. bootstrap 固定底部导航自适应
  6. Java操作MongoDB
  7. SQL 跨服务器数据库增、删、改、查(二)
  8. 转:6款Java转C#的最佳工
  9. 获取考试成绩的sql语句
  10. lodash的中文文档(不全)
  11. java的for循环问题的解决,以及安卓中ListView插入数据的问题
  12. NDK-gdb的错误ERROR(不同于上一篇): Could not extract package's data directory...的解决方法
  13. 【.net 深呼吸】通过标准输入/输出流来完成进程间通信
  14. JavaScript经典作用域问题(转载)
  15. 再次提供一个纯粹通过pl/sql解析json的方法。
  16. [持续更新] 文章列表 Last Update: 8/21/2017
  17. AngularJS+Node.js+socket.io 开发在线聊天室
  18. 活动 Web 页面人机识别验证的探索与实践
  19. 数据中心 CLOS 架构
  20. php 之数组

热门文章

  1. 微信小程序_(校园视)开发视频的展示页_上
  2. 「Luogu P5601」小D与笔试
  3. mumu 连接安卓studio
  4. Linux上Python的安装升级
  5. storm备忘
  6. 解决Git 报错:warning: LF will be replaced by CRLF
  7. 批量替换word内容
  8. 【转】java导出多个excel表格,并压缩成zip输出
  9. 阶段3 3.SpringMVC·_05.文件上传_2 文件上传之传统方式上传代码回顾
  10. 方法的可变参数 params