packageorg.caeit.cloud.dev.util;importjava.io.File;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.nio.charset.Charset;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.Set;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpDelete;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpPut;importorg.apache.http.entity.ContentType;importorg.apache.http.entity.StringEntity;importorg.apache.http.entity.mime.HttpMultipartMode;importorg.apache.http.entity.mime.MultipartEntityBuilder;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.caeit.cloud.dev.entity.HttpResponse; // no usepublic classHttpClientUtil {/*** 发送http get请求*/
public static HttpResponse httpGet(String url,Mapheaders,String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}
String content= null;//since 4.3 不再使用 DefaultHttpClient
CloseableHttpClient closeableHttpClient =HttpClientBuilder.create().build();
HttpGet httpGet= newHttpGet(url);//设置header
if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(),entry.getValue());
}
}
CloseableHttpResponse httpResponse= null;try{
httpResponse=closeableHttpClient.execute(httpGet);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try { //关闭连接、释放资源
closeableHttpClient.close();
}catch(IOException e) {
e.printStackTrace();
}returnresponse;
}/*** 发送 http post 请求,参数以form表单键值对的形式提交。*/
public static HttpResponse httpPostForm(String url,Map params, Mapheaders,String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}//HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
CloseableHttpClient closeableHttpClient =HttpClients.createDefault();
HttpPost httpost= newHttpPost(url);//设置header
if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpost.setHeader(entry.getKey(),entry.getValue());
}
}//组织请求参数
List paramList = new ArrayList ();if(params != null && params.size() > 0){
Set keySet =params.keySet();for(String key : keySet) {
paramList.add(newBasicNameValuePair(key, params.get(key)));
}
}try{
httpost.setEntity(newUrlEncodedFormEntity(paramList, encode));
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String content= null;
CloseableHttpResponse httpResponse= null;try{
httpResponse=closeableHttpClient.execute(httpost);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try { //关闭连接、释放资源
closeableHttpClient.close();
}catch(IOException e) {
e.printStackTrace();
}returnresponse;
}/*** 发送 http post 请求,参数以原生字符串进行提交
*@paramurl
*@paramencode
*@return
*/
public static HttpResponse httpPostRaw(String url,String stringJson,Mapheaders, String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}//HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
CloseableHttpClient closeableHttpClient =HttpClients.createDefault();
HttpPost httpost= newHttpPost(url);//设置header
httpost.setHeader("Content-type", "application/json");if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpost.setHeader(entry.getKey(),entry.getValue());
}
}//组织请求参数
StringEntity stringEntity = newStringEntity(stringJson, encode);
httpost.setEntity(stringEntity);
String content= null;
CloseableHttpResponse httpResponse= null;try{//响应信息
httpResponse =closeableHttpClient.execute(httpost);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try { //关闭连接、释放资源
closeableHttpClient.close();
}catch(IOException e) {
e.printStackTrace();
}returnresponse;
}/*** 发送 http put 请求,参数以原生字符串进行提交
*@paramurl
*@paramencode
*@return
*/
public static HttpResponse httpPutRaw(String url,String stringJson,Mapheaders, String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}//HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
CloseableHttpClient closeableHttpClient =HttpClients.createDefault();
HttpPut httpput= newHttpPut(url);//设置header
httpput.setHeader("Content-type", "application/json");if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpput.setHeader(entry.getKey(),entry.getValue());
}
}//组织请求参数
StringEntity stringEntity = newStringEntity(stringJson, encode);
httpput.setEntity(stringEntity);
String content= null;
CloseableHttpResponse httpResponse= null;try{//响应信息
httpResponse =closeableHttpClient.execute(httpput);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try{
closeableHttpClient.close();//关闭连接、释放资源
} catch(IOException e) {
e.printStackTrace();
}returnresponse;
}/*** 发送http delete请求*/
public static HttpResponse httpDelete(String url,Mapheaders,String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}
String content= null;//since 4.3 不再使用 DefaultHttpClient
CloseableHttpClient closeableHttpClient =HttpClientBuilder.create().build();
HttpDelete httpdelete= newHttpDelete(url);//设置header
if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpdelete.setHeader(entry.getKey(),entry.getValue());
}
}
CloseableHttpResponse httpResponse= null;try{
httpResponse=closeableHttpClient.execute(httpdelete);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try { //关闭连接、释放资源
closeableHttpClient.close();
}catch(IOException e) {
e.printStackTrace();
}returnresponse;
}/*** 发送 http post 请求,支持文件上传*/
public static HttpResponse httpPostFormMultipart(String url,Map params, List files,Mapheaders,String encode){
HttpResponse response= newHttpResponse();if(encode == null){
encode= "utf-8";
}
CloseableHttpClient closeableHttpClient=HttpClients.createDefault();
HttpPost httpost= newHttpPost(url);//设置header
if (headers != null && headers.size() > 0) {for (Map.Entryentry : headers.entrySet()) {
httpost.setHeader(entry.getKey(),entry.getValue());
}
}
MultipartEntityBuilder mEntityBuilder=MultipartEntityBuilder.create();
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mEntityBuilder.setCharset(Charset.forName(encode));//普通参数
ContentType contentType = ContentType.create("text/plain",Charset.forName(encode));//解决中文乱码
if (params != null && params.size() > 0) {
Set keySet =params.keySet();for(String key : keySet) {
mEntityBuilder.addTextBody(key, params.get(key),contentType);
}
}//二进制参数
if (files != null && files.size() > 0) {for(File file : files) {
mEntityBuilder.addBinaryBody("file", file);
}
}
httpost.setEntity(mEntityBuilder.build());
String content= null;
CloseableHttpResponse httpResponse= null;try{
httpResponse=closeableHttpClient.execute(httpost);
HttpEntity entity=httpResponse.getEntity();
content=EntityUtils.toString(entity, encode);
response.setBody(content);
response.setHeaders(httpResponse.getAllHeaders());
response.setReasonPhrase(httpResponse.getStatusLine().getReasonPhrase());
response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
}catch(Exception e) {
e.printStackTrace();
}finally{try{
httpResponse.close();
}catch(IOException e) {
e.printStackTrace();
}
}try { //关闭连接、释放资源
closeableHttpClient.close();
}catch(IOException e) {
e.printStackTrace();
}returnresponse;
}
}