package com.taotao.utils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Package com.taotao.utils
* @User 12428
* @Date 2018/3/23 8:36
*/
public class HttpClientUtil {
private static final StringCHARSET_UTF_8 ="UTF-8";
private static final StringCONTENT_TYPE_JSON ="application/json";
private static final StringCONTENT_TYPE_XML ="text/xml";
/**
* 發(fā)送get請(qǐng)求
? ? * @param url
? ? * @return
? ? */
? ? public static String get(String url) {
String res =null;
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpGet httpGet =new HttpGet(url);
res =execute(httpClient, httpGet);
}finally {
doHttpClientClose(httpClient);
}
return res;
}
/**
* 發(fā)送post請(qǐng)求
? ? * @param url? ? post url
? ? * @param params post參數(shù)
? ? * @return
? ? */
? ? public static String post(String url, Map params) {
String res =null;
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost =httpPostHandler(url, params);
res =execute(httpClient, httpPost);
}finally {
doHttpClientClose(httpClient);
}
return res;
}
/**
* post json數(shù)據(jù)
? ? * @param url
? ? * @param jsonStr
? ? * @return
? ? */
? ? public static String postJson(String url, String jsonStr) {
String res =null;
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost =new HttpPost(url);
StringEntity stringEntity;
try {
stringEntity =new StringEntity(jsonStr);
}catch (UnsupportedEncodingException e) {
return null;
}
httpPost.setHeader("Content-Type",CONTENT_TYPE_JSON);
httpPost.setEntity(stringEntity);
res =execute(httpClient, httpPost);
}finally {
doHttpClientClose(httpClient);
}
return res;
}
/**
* post xml數(shù)據(jù)
? ? * @param url
? ? * @param xmlObj
? ? * @return
? ? */
? ? public static String postXml(String url, Object xmlObj) {
String res =null;
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost =new HttpPost(url);
//解決XStream對(duì)出現(xiàn)雙下劃線的bug
? ? ? ? ? ? XStream xStreamForRequestPostData =new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));
//將要提交給API的數(shù)據(jù)對(duì)象轉(zhuǎn)換成XML格式數(shù)據(jù)Post給API
? ? ? ? ? ? String postDataXML = xStreamForRequestPostData.toXML(xmlObj);
//得指明使用UTF-8編碼修己,否則到API服務(wù)器XML的中文不能被成功識(shí)別
? ? ? ? ? ? StringEntity postEntity =new StringEntity(postDataXML,"UTF-8");
httpPost.addHeader("Content-Type",CONTENT_TYPE_XML);
httpPost.setEntity(postEntity);
res =execute(httpClient, httpPost);
}finally {
doHttpClientClose(httpClient);
}
return res;
}
private static HttpPost httpPostHandler(String url, Map params) {
HttpPost httpPost =new HttpPost(url);
List nvps =new ArrayList<>();
for (Map.Entry entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps,CHARSET_UTF_8));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return httpPost;
}
private static String execute(CloseableHttpClient httpClient, HttpUriRequest httpGetOrPost) {
String res =null;
CloseableHttpResponse response =null;
try {
response = httpClient.execute(httpGetOrPost);
HttpEntity entity = response.getEntity();
res = EntityUtils.toString(entity,CHARSET_UTF_8);
}catch (IOException e) {
e.printStackTrace();
}finally {
doResponseClose(response);
}
return res;
}
private static void doHttpClientClose(CloseableHttpClient httpClient) {
if (httpClient !=null) {
try {
httpClient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
private static void doResponseClose(CloseableHttpResponse response) {
if (response !=null) {
try {
response.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
//? ? public static void main(String[] args) {
//? ? ? ? String url = "";
//? ? ? ? String res = get(url);
//? ? ? ? System.out.println(res);
//? ? }
}