一.https忽略证书

/**
* 用于进行Https请求的HttpClient
*
* @author joey
*
*/
public class SSLClient {
public static CloseableHttpClient createSSLClientDefault(){
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
} }

二.post与get请求

/**
* 利用HttpClient的工具类
*
* @author Joey
*
*/
public class HttpClientUtil { private static String charSet = "UTF-8";
private static CloseableHttpClient httpClient = null;
private static CloseableHttpResponse response = null; /**
* https的post请求
* @param url
* @param jsonstr
* @param charset
* @return
*/
public static String doHttpsPost(String url, String jsonStr) {
try {
httpClient = SSLClient.createSSLClientDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json"); StringEntity se = new StringEntity(jsonStr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se); response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
return EntityUtils.toString(resEntity, charSet);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的post请求(用于key-value格式的参数)
* @param url
* @param param
* @return
*/
public static String doHttpPost(String url,Map<String,String> param){
try {
//请求发起客户端
httpClient = HttpClients.createDefault();
//参数集合
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
//遍历参数并添加到集合
for(Map.Entry<String, String> entry:param.entrySet()){
postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
} //通过post方式访问
HttpPost post = new HttpPost(url);
HttpEntity paramEntity = new UrlEncodedFormEntity(postParams,charSet);
post.setEntity(paramEntity);
response = httpClient.execute(post);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity valueEntity = response.getEntity();
String content = EntityUtils.toString(valueEntity);
//jsonObject = JSONObject.fromObject(content);
return content;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的post请求(用于请求json格式的参数)
* @param url
* @param params
* @return
*/
public static String doHttpPost(String url, String jsonStr) {
try {
httpClient = HttpClients.createDefault(); // 创建httpPost
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json"); StringEntity entity = new StringEntity(jsonStr, charSet);
entity.setContentType("text/json");
entity.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(entity);
//发送post请求
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(httpClient != null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} /**
* http的Get请求
* @param url
* @param param
* @return
*/
public static String doHttpGet(String url,Map<String,String> param) {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null; try {
httpclient = HttpClients.createDefault();
if(param != null && !param.isEmpty()) {
//参数集合
List<NameValuePair> getParams = new ArrayList<NameValuePair>();
for(Map.Entry<String, String> entry:param.entrySet()){
getParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
url +="?"+EntityUtils.toString(new UrlEncodedFormEntity(getParams), "UTF-8");
}
//发送gey请求
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return EntityUtils.toString(response.getEntity());
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(httpclient != null){
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(response != null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
} }

最新文章

  1. java反射技术详解
  2. Javascript自己动手实现getter/setter
  3. ie下获取上传文件全路径
  4. android EditText 只允许输入指定字符
  5. typename使用在模板中区分static成员和类型
  6. python中os模块的常用接口和异常中Exception的运用
  7. Python sys.path.append
  8. pojPots
  9. 如何处理使用js兼容所有浏览器的问题
  10. win2012 配置wamp的若干错误
  11. WebRTC 音频采样算法 附完整C++示例代码
  12. scala的多种集合的使用(3)之遍历集合的方法
  13. 网站性能测试工具 webbench 的安装和使用
  14. js 格式化带时区的日期
  15. git安装以及初始化
  16. 将.csv数据导入到mysql中
  17. Spring Boot + Spring Cloud 构建微服务系统(五):熔断监控面板(Hystrix Dashboard)
  18. 安装edusoho
  19. Jumpserver3.0部署(Centos6.x)
  20. Docker概念学习系列之虚拟化(系统虚拟化和容器虚拟化)

热门文章

  1. log4j 设置日志输出文件的路径
  2. js 只能输入英文和数字,且首位必须是字母,字母总数不能超过3个,总长度不能超过20!
  3. HDU——T1231 最大连续子序列
  4. ASP.Net Cookie总结
  5. NYOJ 298
  6. Solr 搜索的过程和所须要的參数
  7. 分享几个可用的rtsp, http測试url
  8. MMORPG大型游戏设计与开发(server 游戏场景 事件)
  9. Bash脚本中的操作符
  10. 2.linux系统命令详解