public static String getPostMethodWrite(String datas){
HttpClient client = new HttpClient();
//请求地址
String url =getUrl();
PostMethod postMethod = new PostMethod(url);
//设置参数编码为utf-8
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
//构造键值对参数
NameValuePair[] data = { new NameValuePair("data", datas)};
// 把参数值放入postMethod中
postMethod.setRequestBody(data);
//执行
try {
client.executeMethod(postMethod);
//读取内容
byte[] responseBody = postMethod.getResponseBody();
//处理内容
System.out.println(new String(responseBody));
log.info("getStatusLine:"+postMethod.getStatusLine());
log.info("~~~"+postMethod.getResponseBodyAsString());
// System.out.println("statusCode:"+statusCode);
//打印结果页面
String response = new String(postMethod.getResponseBodyAsString().getBytes("utf-8"));
//打印返回的信息
log.info("response:"+response);
return response;
//释放连接
} catch (HttpException e) {
log.error("http协议异常", e);
e.printStackTrace();
return "fasle";
} catch (IOException e) {
log.error("流异常", e);
e.printStackTrace();
return "false";
} finally{
postMethod.releaseConnection();
}
}
httpclient Post请求
最新推荐文章于 2020-10-20 14:17:00 发布