public String xmlHttpPost(String requestInfo, String urlAddress, boolean isProxy,
String proxyHost, int proxyPort, ContentType contentType) {
String responseInfo = null;
InputStream inputResStream = null;
try {
CloseableHttpClient httpClient = null;
if (isProxy) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
}else{
httpClient = HttpClients.createDefault();
}
StringEntity stringEntity = new StringEntity(requestInfo, contentType);
HttpPost post = new HttpPost(urlAddress);
post.setEntity(stringEntity);
log.info("urlAddress: " + urlAddress.trim() + " proxyHost: " + proxyHost.trim() + " proxyPort: " + proxyPort);
CloseableHttpResponse response = httpClient.execute(post);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
inputResStream = entity.getContent();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputResStream));
StringBuffer resBuffer = new StringBuffer();
String resTemp = "";
while ((resTemp = br.readLine()) != null) {
resBuffer.append(resTemp);
}
responseInfo = resBuffer.toString();
} finally {
inputResStream.close();
}
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return responseInfo;
}
HttpClient4.3.5使用代理创建实例并发送请求
最新推荐文章于 2024-01-17 09:42:54 发布