package com.momix.test.util;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
* 简单的httpclient请求
* @author Roger
* @date 2016年6月13日
*/
public class HttpUtils {
/**
* 执行一个HTTP POST请求,返回请求响应的HTML
*
* @param url
* 请求的URL地址
* @param params
* 请求的查询参数,可以为null
* @return 返回请求响应的HTML
*/
public static String doPost(String url, Map<String, String> params) {
String response = null;
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
// 设置Post数据
if (!params.isEmpty()) {
int i = 0;
NameValuePair[] data = new NameValuePair[params.size()];
for (Entry<String, String> entry : params.entrySet()) {
data[i] = new NameValuePair(entry.getKey(), entry.getValue());
i++;
}
postMethod.setRequestBody(data);
}
try {
client.executeMethod(postMethod);
if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
response = postMethod.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
postMethod.releaseConnection();
}
return response;
}
/**
* 执行一个HTTP POST请求,返回请求响应的JSON
*
* @param url
* 请求的URL地址
* @param json
* 请求的参数
* @return
*/
@SuppressWarnings({ "deprecation", "resource" })
public static String doPost(String url, JSONObject json) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
String result = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(res.getEntity());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
/**
* 执行一个HTTP GET请求,返回请求响应的HTML
*
* @param url
* 请求的URL地址
* @return 返回请求响应的HTML
*/
public static String doGet(String url) {
String response = null;
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
try {
client.executeMethod(getMethod);
if (getMethod.getStatusCode() == HttpStatus.SC_OK) {
response = getMethod.getResponseBodyAsString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
return response;
}
public static void main(String[] args) {
System.out.println(HttpUtils.doGet("http://localhost:8080/wx/wxConfig"));
String url = "http://localhost:8080/index/login";
JSONObject json = new JSONObject();
json.put("openId", "ocSa0uEXN_Rc_UWFeMDkPhqDlKTg");
String result = HttpUtils.doPost(url, json);
System.out.println(result);
}
}原文地址:http://10960988.blog.51cto.com/10950988/1788630