HTTP POST方式 这种方式写存在什么问题?
http://xx.xx.xx.xx:xxxx/aa?Body=msg
String httpRes = "";// 获取结果
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String url = "http://xx.x.x:x/aa";
String msg = “”
try {
httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(6000).setConnectTimeout(3000).build();
post.setConfig(requestConfig);
String Body= URLEncoder.encode(msg, "UTF-8");
//构建内容
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("Body", Body));
HttpEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
// 发送的数据请求
post.addHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
post.setEntity(entity);
try {
response = httpClient.execute(post);
// 从response中取出HttpEntity对象
HttpEntity entity1 = response.getEntity();
httpRes = EntityUtils.toString(entity1, "UTF-8");
EntityUtils.consume(response.getEntity());// 完全消耗
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != response)
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放链接
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return httpRes;