216 lines
8.1 KiB
Java
216 lines
8.1 KiB
Java
package com.tenwa.gps;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import org.apache.http.HttpEntity;
|
||
import org.apache.http.HttpResponse;
|
||
import org.apache.http.NameValuePair;
|
||
import org.apache.http.client.HttpClient;
|
||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||
import org.apache.http.client.methods.HttpDelete;
|
||
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.utils.URIBuilder;
|
||
import org.apache.http.entity.StringEntity;
|
||
import org.apache.http.impl.client.HttpClientBuilder;
|
||
import org.apache.http.message.BasicNameValuePair;
|
||
import org.apache.http.util.EntityUtils;
|
||
public class HttpClientSUtil {
|
||
|
||
private static HttpClient httpClient = HttpClientBuilder.create().build();;
|
||
|
||
//patch请求
|
||
public static Map<String,String> doPatchJson(String url, String params) throws Exception{
|
||
|
||
HttpPatch patch = new HttpPatch(url);
|
||
|
||
//设置参数以及编码
|
||
StringEntity entity = new StringEntity(params,"UTF-8");
|
||
//设置请求头,服务器接收参数的格式以及编码格式
|
||
entity.setContentType("application/json;charset=UTF-8");
|
||
//发起httppatch请求
|
||
patch.setEntity(entity);
|
||
HttpResponse response = httpClient.execute(patch);
|
||
// 判断返回状态为200时解析返回数据
|
||
System.err.println(response.getStatusLine().getStatusCode());
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
//put请求,参数为json
|
||
public static Map<String, String> doPutJson(String url, String json) throws Exception{
|
||
HttpPut put = new HttpPut(url);
|
||
//设置参数以及编码
|
||
StringEntity entity = new StringEntity(json,"UTF-8");
|
||
//设置请求头,服务器接收参数的格式以及编码格式
|
||
entity.setContentType("application/json;charset=UTF-8");
|
||
//发起httpPut请求
|
||
put.setEntity(entity);
|
||
HttpResponse response = httpClient.execute(put);
|
||
//返回结果解析
|
||
HttpEntity httpEntity =response.getEntity();
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
//Post请求,参数为json
|
||
public static Map<String, String> doPostJson(String url, String json) throws Exception{
|
||
|
||
HttpPost post = new HttpPost(url);
|
||
//设置参数以及编码
|
||
StringEntity entity = new StringEntity(json,"UTF-8");
|
||
//设置请求头,服务器接收参数的格式以及编码格式
|
||
entity.setContentType("application/json;charset=UTF-8");
|
||
//发起httpPost请求
|
||
post.setEntity(entity);
|
||
HttpResponse response = httpClient.execute(post);
|
||
//返回结果解析
|
||
HttpEntity httpEntity =response.getEntity();
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
//Post请求,参数为xml
|
||
public static Map<String, String> doPostXml(String url, String xml) throws Exception{
|
||
|
||
HttpPost post = new HttpPost(url);
|
||
//设置参数以及编码
|
||
StringEntity entity = new StringEntity(xml,"UTF-8");
|
||
//设置请求头,服务器接收参数的格式以及编码格式
|
||
entity.setContentType("application/xml;charset=UTF-8");
|
||
//发起httpPost请求
|
||
post.setEntity(entity);
|
||
HttpResponse response = httpClient.execute(post);
|
||
//返回结果解析
|
||
HttpEntity httpEntity =response.getEntity();
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
// post请求,参数为Map
|
||
/**
|
||
*
|
||
* @param url //请求路径
|
||
* @param params //请求参数
|
||
* @param appkey //身份验证秘钥
|
||
* @param encode //编码格式
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static Map<String, String> doPost(String url,Map<String,String> params,String appkey,String encode) throws Exception{
|
||
|
||
HttpPost post = new HttpPost(url);
|
||
//设置参数以及编码
|
||
if (null != params) {
|
||
//添加请求参数
|
||
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
|
||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||
}
|
||
|
||
//设置编码格式以及服务器解码格式
|
||
if(encode==null){
|
||
encode ="UTF-8";
|
||
}
|
||
// 构造一个form表单式的实体
|
||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,encode);
|
||
|
||
// 将请求实体设置到httpPost对象中
|
||
post.setEntity(formEntity);
|
||
if(appkey!=null){
|
||
//设置请求头,添加身份验证码
|
||
post.addHeader("appkey",appkey);
|
||
}
|
||
post.addHeader("Content-Type","application/x-www-form-urlencoded");
|
||
}
|
||
// 执行请求
|
||
HttpResponse response = httpClient.execute(post);
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
// post请求,参数为Map,返回为byte[]
|
||
/**
|
||
*
|
||
* @param url //请求路径
|
||
* @param params //请求参数
|
||
* @param appkey //身份验证秘钥
|
||
* @param encode //编码格式
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static Map<String, String> doPostRbyte(String url,Map<String,String> params,String appkey,String encode) throws Exception{
|
||
|
||
HttpPost post = new HttpPost(url);
|
||
//设置参数以及编码
|
||
if (null != params) {
|
||
//添加请求参数
|
||
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
|
||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||
}
|
||
|
||
//设置编码格式以及服务器解码格式
|
||
if(encode==null){
|
||
encode ="UTF-8";
|
||
}
|
||
// 构造一个form表单式的实体
|
||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,encode);
|
||
|
||
// 将请求实体设置到httpPost对象中
|
||
post.setEntity(formEntity);
|
||
if(appkey!=null){
|
||
//设置请求头,添加身份验证码
|
||
post.addHeader("appkey",appkey);
|
||
}
|
||
}
|
||
// 执行请求
|
||
HttpResponse response = httpClient.execute(post);
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
|
||
|
||
//HTTPGet请求
|
||
public static Map<String, String> doGet(String url,Map<String,String> params,String appkey) throws Exception{
|
||
//将参数写到请求路径上
|
||
if(null != params){
|
||
URIBuilder builder = new URIBuilder(url);
|
||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||
builder.setParameter(entry.getKey(), entry.getValue());
|
||
}
|
||
url = builder.build().toString();
|
||
}
|
||
//创建HPPTGet对象
|
||
HttpGet get = new HttpGet(url);
|
||
|
||
if(appkey!=null){
|
||
//设置请求头,添加身份验证码
|
||
get.addHeader("appkey",appkey);
|
||
}
|
||
// 执行请求
|
||
HttpResponse response = httpClient.execute(get);
|
||
Map<String,String> resultMap = new HashMap<String, String>();
|
||
resultMap.put("data",EntityUtils.toString(response.getEntity(),"UTF-8"));
|
||
resultMap.put("status",String.valueOf(response.getStatusLine().getStatusCode()));
|
||
return resultMap;
|
||
}
|
||
}
|