Java发送HTTP的POST请求

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
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.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class HttpClientUtil {

public static final Log logger = LogFactory.getLog("httpclient");


public static String httpReader(String url, String code){
logger.info("GetPage:"+url);

HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);

String result = null;
try {
client.executeMethod(method);
int status = method.getStatusCode();
if (status == HttpStatus.SC_OK) {
result = method.getResponseBodyAsString();
} else {
logger.info("Method failed: " + method.getStatusLine());
}
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
logger.info("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
logger.info("发生网络异常!");
e.printStackTrace();
} finally{
// 释放连接
if(method!=null)method.releaseConnection();
method = null;
client = null;
}
return result;
}

public static String httpPost(String url, Map paramMap, String code) {
logger.info("GetPage:"+url);
String content = null;
if (url == null || url.trim().length() == 0 || paramMap == null
|| paramMap.isEmpty())
return null;
HttpClient httpClient = new HttpClient();
//设置header
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");

PostMethod method = new PostMethod(url);
Iterator it = paramMap.keySet().iterator();


while (it.hasNext()) {
String key = it.next() + "";
Object o = paramMap.get(key);
if (o != null && o instanceof String) {
method.addParameter(new NameValuePair(key, o.toString()));
}
if (o != null && o instanceof String[]) {
String[] s = (String[]) o;
if (s != null)
for (int i = 0; i < s.length; i++) {
method.addParameter(new NameValuePair(key, s[i]));
}
}
}
try {

int statusCode = httpClient.executeMethod(method);

System.out.println("httpClientUtils::statusCode="+statusCode);

logger.info(method.getStatusLine());
content = new String(method.getResponseBody(), code);

} catch (Exception e) {
logger.info("time out");
e.printStackTrace();
} finally {
if(method!=null)method.releaseConnection();
method = null;
httpClient = null;
}
return content;

}

public static String httpPost(String url, Map paramMap) {
//编码:GB2312
return HttpClientUtil.httpPost(url, paramMap, "GB2312");
}

public static void main(String[] args)throws Exception
{

String URL="http://127.0.0.1:8001/app/test";

Map map = new HashMap();
map.put("param1","value1");
map.put("param2","value2");
map.put("param3","value3");
map.put("param4","value4");

String ret = HttpClientUtil.httpPost(URL, map);

System.out.println("ret::"+ret);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值