标签:
package com.gop.gplus.trade.common.utils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by zhangtan on 2016/9/23.
*/
public class HttpClientUtil {
public static String post(Map<String, Object> sParaTemp, String url) throws Exception{
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
List<String> keys = new ArrayList<String>(sParaTemp.keySet());
NameValuePair[] param = new NameValuePair[keys.size()+1];
for (int i = 0; i < keys.size(); i++) {
String name = keys.get(i);
Object object = sParaTemp.get(name);
String value = "";
if (object != null) {
value = (String) sParaTemp.get(name);
}
//添加参数
param[i] = new NameValuePair(name, value);
post.setParameter(param[i].getName(),param[i].getValue());
//System.out.println(param[i].getName());
}
HttpMethod method = post;
httpClient.executeMethod(method);
String response = method.getResponseBodyAsString();
post.releaseConnection();
return response;
}
}
标签:
原文地址:http://www.cnblogs.com/zhangtan/p/5900392.html