首页 > 代码库 > HttpUtil
HttpUtil
package com.eaju.util;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ByteArrayEntity;
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 org.springframework.http.HttpMethod;
public class HttpUtil {
public final static String APPLICATION_PROTOBUF = "application/x-protobuf;charset=UTF-8";
public final static String APPLICATION_JSON="application/json;charset=UTF-8";
public final static String APPLICATION_XML="application/xml;charset=UTF-8";
public final static String TEXT_PLAIN="text/plain;charset=UTF-8";
/**
*
* @param url
* @param params
* @param accept
* @return
* @throws IOException
*/
public static byte[] get(String url,Map<String,String> params,String accept) throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
if(params != null){
StringBuilder paramUrl = new StringBuilder();
for(String key:params.keySet()){
paramUrl.append(URLEncoder.encode(key,"UTF-8")).
append("=").
append(URLEncoder.encode(params.get(key),"UTF-8")).
append("&");
}
url+=url+"?"+paramUrl.substring(0,paramUrl.length()-1);
}
HttpGet httpGet =new HttpGet(url);
httpGet.setHeader("Accept", accept);
CloseableHttpResponse response =httpclient.execute(httpGet);
byte[] result = EntityUtils.toByteArray(response.getEntity());
response.close();
httpclient.close();
return result;
}
/**
*
* @param url
* @param accept
* @return
* @throws IOException
*/
public static byte[] get(String url,String accept) throws IOException{
return get(url,null,accept);
}
/**
*
* @param url
* @param params
* @param accept
* @return
* @throws IOException
*/
public static byte[] post(String url,Map<String,String> params,String accept)throws IOException{
return execute(url,HttpMethod.POST,params,accept);
}
/**
*
* @param url
* @param params
* @param accept
* @return
* @throws IOException
*/
public static byte[] patch(String url,Map<String,String> params,String accept)throws IOException{
return execute(url,HttpMethod.PATCH,params,accept);
}
/**
*
* @param url
* @param params
* @param accept
* @return
* @throws IOException
*/
public static byte[] put(String url,Map<String,String> params,String accept)throws IOException{
return execute(url,HttpMethod.PUT,params,accept);
}
/**
*
* @param url
* @param data
* @param contentType
* @param accept
* @return
* @throws IOException
*/
public static byte[] post(String url,byte [] data,String contentType,String accept)throws IOException{
return execute(url,HttpMethod.POST,data,contentType,accept);
}
/**
*
* @param url
* @param data
* @param contentType
* @param accept
* @return
* @throws IOException
*/
public static byte[] put(String url,byte [] data,String contentType,String accept)throws IOException{
return execute(url,HttpMethod.PUT,data,contentType,accept);
}
/**
*
* @param url
* @param data
* @param contentType
* @param accept
* @return
* @throws IOException
*/
public static byte[] patch(String url,byte [] data,String contentType,String accept)throws IOException{
return execute(url,HttpMethod.PATCH,data,contentType,accept);
}
/**
*
* @param url
* @param accept
* @return
* @throws IOException
*/
public static byte[] delete(String url,String accept)throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.setHeader("Accept", accept);
CloseableHttpResponse response =httpclient.execute(httpDelete);
byte[] result =EntityUtils.toByteArray(response.getEntity());
response.close();
httpclient.close();
return result;
}
/**
*
* @param url
* @param method
* @param params
* @param accept
* @return
* @throws IOException
*/
private static byte[] execute(String url,HttpMethod method,Map<String,String> params,String accept)throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestBuilder requestBuilder;
switch (method){
case POST:
requestBuilder = RequestBuilder.post(url);
if(params != null){
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
for(String key:params.keySet()){
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity encodedFormEntity= new UrlEncodedFormEntity(nvps,Charset.forName("UTF-8"));
requestBuilder.setEntity(encodedFormEntity);
}
break;
case PUT:
if(params != null){
for(String key:params.keySet()){
url+="&"+URLEncoder.encode(key,"UTF-8")+"="+ URLEncoder.encode(params.get(key),"UTF-8");
}
url=url.replaceFirst("&","?");
}
System.out.println(url);
requestBuilder = RequestBuilder.put(url);
break;
case PATCH:
requestBuilder = RequestBuilder.patch(url);
requestBuilder.setHeader("Accept", accept);
requestBuilder.setCharset(Charset.forName("UTF-8"));
if(params != null){
for(String key:params.keySet()){
requestBuilder.addParameter(new BasicNameValuePair(key, params.get(key)));
}
}
break;
default:
requestBuilder = RequestBuilder.post(url);
if(params != null){
List<NameValuePair> nvps = new ArrayList <NameValuePair>();
for(String key:params.keySet()){
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
UrlEncodedFormEntity encodedFormEntity= new UrlEncodedFormEntity(nvps,Charset.forName("UTF-8"));
requestBuilder.setEntity(encodedFormEntity);
}
break;
}
CloseableHttpResponse response =httpclient.execute(requestBuilder.build());
byte[] result =EntityUtils.toByteArray(response.getEntity());
response.close();
httpclient.close();
return result;
}
/**
*
* @param url
* @param method
* @param data
* @param contentType
* @param accept
* @return
* @throws IOException
*/
private static byte[] execute(String url,HttpMethod method,byte [] data,String contentType,String accept)throws IOException{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpEntityEnclosingRequestBase httpRequest;
switch (method){
case POST:
httpRequest = new HttpPost(url);
break;
case PUT:
httpRequest = new HttpPut(url);
break;
case PATCH:
httpRequest = new HttpPatch(url);
break;
default:
httpRequest = new HttpPost(url);
}
httpRequest.setHeader("Content-Type", contentType);
httpRequest.setHeader("Accept", accept);
if(data != null){
httpRequest.setEntity(new ByteArrayEntity(data));
}
CloseableHttpResponse response =httpclient.execute(httpRequest);
byte[] result =EntityUtils.toByteArray(response.getEntity());
response.close();
httpclient.close();
return result;
}
}
HttpUtil