Java工具类-HttpUtil
package com.sh.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; 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.ClientProtocolException;
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;
public class HttpsUtil {
/**
* 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
*
* @param base
* @return
*/
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,String string) {
}
public void checkServerTrusted(X509Certificate[] xcs,String string) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
} /**
* http get请求
* @param url 请求地址
* @param headers 请求头
* @param pmap 请求参数
* @param ishttps 是否使用https true:使用 false:不使用
* @return
*/
public static String sendHttpsGet(String url,Map<String, String> headers,Map<String, String> pmap,boolean ishttps){
HttpClient client = new DefaultHttpClient();
if(ishttps){
client = wrapClient(client);
}
// 实例化HTTP方法
HttpGet get = new HttpGet();
for(String keyh : headers.keySet()) {
get.setHeader(keyh,headers.get(keyh));
}
String params = "";
for(String keyp : pmap.keySet()) {
params += "&" + keyp + "=" + pmap.get(keyp);
}
url += params.replaceAll("^&", "?");
String result ="";
try {
get.setURI(new URI(url));
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
// TODO: handle exception
}
try {
result=new String(result.getBytes("ISO-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result; } /**
* http post请求
* @param url 请求地址
* @param headers 请求头
* @param pmap 请求参数
* @param ishttps 是否使用https true:使用 false:不使用
* @return HttpEntity 使用org.apache.http.util.EntityUtils.toString()、com.alibaba.fastjson.JSON.parseObject()解析
*/
public static HttpEntity sendHttpsPost(String url,Map<String, String> headers,Map<String, String> pmap,boolean ishttps){
HttpClient client = new DefaultHttpClient();
if(ishttps){
client = wrapClient(client);
}
HttpPost postrequest = new HttpPost(url);
HttpEntity entity = null;
try {
for(String keyh : headers.keySet()) {
postrequest.setHeader(keyh, headers.get(keyh));
}
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for(String key : pmap.keySet()) {
nvps.add(new BasicNameValuePair(key,pmap.get(key)));
} postrequest.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
HttpResponse execute = client.execute(postrequest);
entity = execute.getEntity();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return entity;
}
public static void main(String[] args) {
String t="{'uuid':'ec85f716-7647-439b-8d28-34cb21f95098','session':'4d590d61-b3e1-464f-b89a-03b69a90e040','label':null,'type':'pub','permanent':true,'data':null,'time_created':'2017-10-18T02:18:48.910656Z','live_days':15,'expire_in':'2017-11-02T02:18:48.910Z'}";
net.sf.json.JSONArray jsonarr = net.sf.json.JSONArray.fromObject(t);
for(int i=0;i<jsonarr.size();i++){
net.sf.json.JSONObject jsonObject = jsonarr.getJSONObject(i);
System.out.println(jsonObject.get("uuid"));
} } }
Java工具类-HttpUtil的更多相关文章
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
- 第一章 Java工具类目录
在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Java工具类:给程序增加版权信息
我们九天鸟的p2p网贷系统,基本算是开发完成了. 现在,想给后端的Java代码,增加版权信息. 手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...
- 常用高效 Java 工具类总结
一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...
随机推荐
- java三大框架——Struts + Hibernate + Spring
Struts主要负责表示层的显示 Spring利用它的IOC和AOP来处理控制业务(负责对数据库的操作) Hibernate主要是数据持久化到数据库 再用jsp的servlet做网页开发的时候有个 w ...
- Apache Shiro漏洞复现
利用burp dns进行检测,脚本如下: import sys import uuid import base64 import subprocess from Crypto.Cipher impor ...
- VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作
VMwarevSphere Client 链接 vCenter Server中的主机,开启虚拟机提示:在主机当前连接状况下不允许执行该操作很多原因都可以导致该问题出现,例如 vCenter Serve ...
- jvm出现OutOfMemoryError时处理方法/jvm原理和优化参考
The heap stores all of the objects created by your java program.The heap's contents is monitored by ...
- Java ==和equals的区别
首先了解默认equals方法实现代码 public boolean equals(Object obj) { return (this == obj); } 1.== (1)对于基本数据类型的变量,& ...
- C#在Oralce环境执行查询时报"Arithmetic operation resulted in an overflow"
问题描述:C#代码在Oralce环境执行分组求和的Sql时报错,提示“Arithmetic operation resulted in an overflow”,即算术运算导致溢出 (1).执行Sql ...
- 3.Fech_feed
import tensorflow as tf # Fetch:可以在session中同时计算多个tensor或执行多个操作 # 定义三个常量 input1 = tf.constant(3.0) in ...
- Tomcat基础知识
介绍Tomcat之前先介绍下Java相关的知识. 各常见组件: 1.服务器(server):Tomcat的一个实例,通常一个JVM只能包含一个Tomcat实例:因此,一台物理服务器上可以在启动多个JV ...
- redis实现排行榜功能
目录 加入排行榜 操作排行榜 redis的zset可以很方便地用来实现排行榜功能,下面简单介绍python如何使用redis实现排行榜功能 加入排行榜 获取redis实例 import redis m ...
- What is Double 11 in China? Is it a famous festival?
"1" means single, 11th, November is quadruple single!! What a tragedy for those single you ...