private static String httpPost(String url) throws Exception {
Map<String,String> param=new HashMap<String, String>();
Map jsonmap = new HashMap();
jsonmap.put("username", "111");
jsonmap.put("passwd","111");
//net.sf.json.JSONObject 将Map转换为JSON方法
JSONObject json = JSONObject.fromObject(jsonmap);
param.put("json",json.toString());
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
if (param != null) {
for (Object key : param.keySet()) {
pairs.add(new NameValuePair(String.valueOf(key), MapUtils.getString(param, key, "")));
}
}
// 1、创建一个htt客户端
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
postMethod.addRequestHeader("charset", "UTF-8");
postMethod.setRequestBody(pairs.toArray(new NameValuePair[0]));
int status = httpClient.executeMethod(postMethod);
LOGGER.debug("http连接状态status:" + status);
String result = postMethod.getResponseBodyAsString();
return result;
}
使用httpclient发送post 请求form表单格式的参数获取返回值
最新推荐文章于 2024-03-25 11:37:56 发布
1025

被折叠的 条评论
为什么被折叠?



