一.基本上这样,如有不同大体修改一下就可以
写一个这样的HttpClientUtils 工具类
public class HttpClientUtils {
/**
* post请求
* @param url
* @param authorValue
* @param json
* @return
*/
public static JSONObject doPost(String url, String authorValue, JSONObject json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if(StringUtils.isNotEmpty(authorValue)){
post.setHeader("Authorization",authorValue);
}
JSONObject response = null;
try {
response = getJsonObject(json, client, post);
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
public static JSONObject doPost(String url, String authorValue, JSONArray json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if(StringUtils.isNotEmpty(authorValue)){
post.setHeader("Authorization",authorValue);
}
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity);// 返回json格式:
response = JSONObject.fromObject(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
private static JSONObject getJsonObject(JSONObject json, DefaultHttpClient client, HttpEntityEnclosingRequestBase base) throws IOException {
JSONObject response;StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
base.setEntity(s);
HttpResponse res = client.execute(base);
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity);// 返回json格式:
response = JSONObject.fromObject(result);
return response;
}
/**
* put请求
* @param url
* @param authorValue
* @param json
* @return
*/
public static JSONObject doPut(String url, String authorValue, JSONObject json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
if(StringUtils.isNotEmpty(authorValue)){
put.setHeader("Authorization",authorValue);
}
JSONObject response = null;
try {
response = getJsonObject(json, client, put);
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
/**
* delete请求
* @param url
* @param authorValue
* @param dataForm
* @return
*/
public static JSONObject doDelete(String url, String authorValue,Map<String,Object> dataForm){
HttpClient httpClient = new HttpClient();
DeleteMethod deleteMethod = new DeleteMethod(url);
if(StringUtils.isNotEmpty(authorValue)){
deleteMethod.setRequestHeader("Authorization",authorValue);
}
List<NameValuePair> data = new ArrayList<NameValuePair>();
if(dataForm!=null){
Set<String> keys = dataForm.keySet();
for(String key:keys){
NameValuePair nameValuePair = new NameValuePair(key, (String) dataForm.get(key));
data.add(nameValuePair);
}
}
deleteMethod.setQueryString(data.toArray(new NameValuePair[0]));
try {
int statusCode = httpClient.executeMethod(deleteMethod);
// Read the response body.
byte[] responseBody = deleteMethod.getResponseBody();
return JSONObject.fromObject(new String(responseBody));
} catch (IOException e) {
e.printStackTrace();
}finally {
deleteMethod.releaseConnection();
}
return null;
}
/**
* get 请求
* @param url
* @param authorValue
* @param para
* @return
*/
public static JSONObject doGet(String url,String authorValue,Map<String, String> para){
JSONObject response = null;
try{
DefaultHttpClient client = new DefaultHttpClient();
URIBuilder builder = new URIBuilder(url);
Set<String> set = para.keySet();
for(String key: set){
builder.setParameter(key, para.get(key));
}
HttpGet request = new HttpGet(builder.build());
if(StringUtils.isNotEmpty(authorValue)){
request.setHeader("Authorization",authorValue);
}
request.setHeader("ContentTye","application/json");
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(6000)
.setConnectTimeout(6000)
.setConnectionRequestTimeout(6000).build();
request.setConfig(requestConfig);
HttpResponse res = client.execute(request);
// if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
// }
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
}