packagecom.sh.util;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.net.URI;importjava.security.cert.X509Certificate;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjavax.net.ssl.SSLContext;importjavax.net.ssl.TrustManager;importjavax.net.ssl.X509TrustManager;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.conn.ClientConnectionManager;importorg.apache.http.conn.scheme.Scheme;importorg.apache.http.conn.scheme.SchemeRegistry;importorg.apache.http.conn.ssl.SSLSocketFactory;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;public classHttpsUtil {/*** 获取可信任https链接,以避免不受信任证书出现peer not authenticated异常
*
*@parambase
*@return
*/
public staticHttpClient wrapClient(HttpClient base) {try{
SSLContext ctx= SSLContext.getInstance("TLS");
X509TrustManager tm= newX509TrustManager() {public voidcheckClientTrusted(X509Certificate[] xcs,String string) {
}public voidcheckServerTrusted(X509Certificate[] xcs,String string) {
}publicX509Certificate[] getAcceptedIssuers() {return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf= newSSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm=base.getConnectionManager();
SchemeRegistry sr=ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));return newDefaultHttpClient(ccm, base.getParams());
}catch(Exception ex) {
ex.printStackTrace();return null;
}
}/*** http get请求
*@paramurl 请求地址
*@paramheaders 请求头
*@parampmap 请求参数
*@paramishttps 是否使用https true:使用 false:不使用
*@return
*/
public static String sendHttpsGet(String url,Map headers,Map pmap,booleanishttps){
HttpClient client= newDefaultHttpClient();if(ishttps){
client=wrapClient(client);
}//实例化HTTP方法
HttpGet get = newHttpGet();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(newURI(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();
}returnresult;
}/*** http post请求
*@paramurl 请求地址
*@paramheaders 请求头
*@parampmap 请求参数
*@paramishttps 是否使用https true:使用 false:不使用
*@returnHttpEntity 使用org.apache.http.util.EntityUtils.toString()、com.alibaba.fastjson.JSON.parseObject()解析*/
public static HttpEntity sendHttpsPost(String url,Map headers,Map pmap,booleanishttps){
HttpClient client= newDefaultHttpClient();if(ishttps){
client=wrapClient(client);
}
HttpPost postrequest= newHttpPost(url);
HttpEntity entity= null;try{for(String keyh : headers.keySet()) {
postrequest.setHeader(keyh, headers.get(keyh));
}
List nvps = new ArrayList();for(String key : pmap.keySet()) {
nvps.add(newBasicNameValuePair(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();
}returnentity;
}public static voidmain(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
net.sf.json.JSONObject jsonObject=jsonarr.getJSONObject(i);
System.out.println(jsonObject.get("uuid"));
}
}
}