您的位置:首页 > 理论基础 > 计算机网络

httpclient请求方法

2015-08-29 10:24 531 查看
/**
* httpclient请求方法
* @param url 请求地址
* @param paramMap 请求参数
* @param ent 编码格式 gbk、utf-8
* @return String 返回字符串
*/
public static String httpClientRQ(String url,Map<String,String> paramMap,String ent){
String rs = "";
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod(url);
try{
int paramLength = paramMap.size();
NameValuePair[] nps = new NameValuePair[paramLength];
int index = 0;
for(Map.Entry<String, String> map:paramMap.entrySet()){
String key = map.getKey();
String value = map.getValue();
//                Object objvalue = map.getValue();
//                String value = "";
//                if(objvalue!=null){
//                    value = objvalue.toString();
//                }
nps[index] = new NameValuePair();
nps[index].setName(key);
nps[index].setValue(value);
index++;
}

post.setRequestBody(nps);
post.getParams().setContentCharset(ent);
httpClient.getParams().setConnectionManagerTimeout(10000);
httpClient.getParams().setSoTimeout(10000);
httpClient.executeMethod(post);

rs = post.getResponseBodyAsString();
}catch(Exception e){
e.printStackTrace();
}finally{
post.releaseConnection();

}

return rs;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: