情景: 调远程接口,Postman已测试通过,在代码里用httpclient的 get/post方法 都不能调用成功(NoHttpResponseException,xxhost failed to respond),排除白名单问题,且发现用JDK的HttpURLConnection可以调通,但是只能GET方式调通。
处理方法:不知道为什么httpclient不行,怀疑远程服务器不支持? 只能通过HttpURLConnection来调了,修改了下Post方法,就可以了。
HttpURLConnection代码:
package com.wonders.cop.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @Date : 2018/7/6 15:40
* @Description : HttpClient工具类
*/
public class HttpClientUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class);
private static final String REQUEST_METHOD_GET = "GET";
private static final String REQUEST_METHOD_POST = "POST";
private static final String DEFAULT_CHARSET = "UTF-8";
private static final int DEFAULT_CONNECTION_TIMEOUT = 5;
private static final int DEFAULT_READ_TIMEOUT = 10;
private static final String[][] DEFAULT_HEADERS = {{"Content-Type", "application/json"}};
private static final int ONE_THOUSAND = 1000;
public static String post(String url, String body, String[][] headers, Integer connectionTimeout, Integer readTimeout, String charset) {
if (headers == null) {
headers = DEFAULT_HEADERS;
}
if (connectionTimeout == null) {
connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
}
if (readTimeout == null) {
readTimeout = DEFAULT_READ_TIMEOUT;
}
if (StringUtils.isBlank(charset)) {
charset = DEFAULT_CHARSET;
}
URL uri;
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try {
uri = new URL(url);
httpURLConnection = (HttpURLConnection) uri.openConnection();
httpURLConnection.setRequestMethod(REQUEST_METHOD_POST);
httpURLConnection.setConnectTimeout(ONE_THOUSAND * connectionTimeout); // 连接超时时间
httpURLConnection.setReadTimeout(ONE_THOUSAND * readTimeout); // 响应超时时间
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
// 设置Http报文请求头
for (String[] header : headers) {
httpURLConnection.addRequestProperty(header[0], header[1]);
}
// 提交Http请求参数
httpURLConnection.connect();
if (null != body) {
httpURLConnection.getOutputStream().write(body.getBytes(charset));
httpURLConnection.getOutputStream().flush();
httpURLConnection.getOutputStream().close();
}
// 读取响应参数
int responseCode = httpURLConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset));
StringBuilder response = new StringBuilder();
String line = bufferedReader.readLine();
while (null != line) {
response.append(line);
line = bufferedReader.readLine();
}
return response.toString();
} else {
LOGGER.info("调用失败, responseCode=" + responseCode);
}
} catch (Exception e) {
LOGGER.error("调用异常, Exception:", e);
} finally {
if (null != httpURLConnection) {
httpURLConnection.disconnect();
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
LOGGER.error("BufferedReader 关闭异常:", e);
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.error("InputStream 关闭异常:", e);
}
}
}
return null;
}
public static String get(String url, String[][] headers, Integer connectionTimeout, Integer readTimeout, String charset) {
if (connectionTimeout == null) {
connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
}
if (readTimeout == null) {
readTimeout = DEFAULT_READ_TIMEOUT;
}
if (StringUtils.isBlank(charset)) {
charset = DEFAULT_CHARSET;
}
URL uri;
HttpURLConnection httpURLConnection = null;
BufferedReader bufferedReader = null;
InputStream inputStream = null;
try {
uri = new URL(url);
httpURLConnection = (HttpURLConnection) uri.openConnection();
httpURLConnection.setRequestMethod(REQUEST_METHOD_GET);
httpURLConnection.setConnectTimeout(ONE_THOUSAND * connectionTimeout);
httpURLConnection.setReadTimeout(ONE_THOUSAND * readTimeout);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 设置Http报文请求头
if (headers != null) {
for (String[] header : headers) {
httpURLConnection.addRequestProperty(header[0], header[1]);
}
}
// 提交Http请求参数
httpURLConnection.connect();
// 读取响应参数
int responseCode = httpURLConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == responseCode) {
inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset));
StringBuilder response = new StringBuilder();
String line = bufferedReader.readLine();
while (null != line) {
response.append(line);
line = bufferedReader.readLine();
}
return response.toString();
} else {
LOGGER.info("调用失败, responseCode=" + responseCode);
}
} catch (Exception e) {
LOGGER.error("调用异常, Exception:", e);
} finally {
if (null != httpURLConnection) {
httpURLConnection.disconnect();
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
LOGGER.error("BufferedReader关闭异常:", e);
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.error("InputStream关闭异常:", e);
}
}
}
return null;
}
/**
* @Author : jrc
* @Date : 2019/2/21 17:30
* @Description : form-data UTF-8
*/
public static String postDataForm(String uri, Map<String, String> map) {
return postDataForm(uri, map, "UTF-8");
}
/**
* @Author : jrc
* @Date : 2019/2/26 17:34
* @Description : form-data
*/
public static String postDataForm(String uri, Map<String, String> map, String charset) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost();
List<BasicNameValuePair> pairs = new ArrayList<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
httpPost.setURI(new URI(uri));
httpPost.setEntity(new UrlEncodedFormEntity(pairs, charset));
HttpResponse response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
LOGGER.info("statusCode : {}", statusCode);
HttpEntity entity = response.getEntity();
return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
} catch (URISyntaxException e) {
LOGGER.error("setURI 异常", e);
return null;
} catch (UnsupportedEncodingException e) {
LOGGER.error("setEntity 异常", e);
return null;
} catch (IOException e) {
LOGGER.error("IO 异常", e);
return null;
} finally {
httpPost.releaseConnection();
}
}
/**拼串 URL参数
* @Author : wj
* @Date : 2019/4/28 17:34
*/
public static String appendUrlParam(Map<String, String> map) {
StringBuilder sb = new StringBuilder();
try {
Set<String> keySet = map.keySet();
int i = 0;
for (String key : keySet) {
if (StringUtils.isNotEmpty(map.get(key))) {
if (++i == 1) {
sb.append("?").append(key).append("=").append(URLEncoder.encode(map.get(key), "UTF-8"));
} else {
sb.append("&").append(key).append("=").append(URLEncoder.encode(map.get(key), "UTF-8"));
}
}
}
} catch (UnsupportedEncodingException e) {
LOGGER.error("UTF-8转码错误",e);
}
return sb.toString();
}
}
调用方式:
String parms =HttpClientUtil.appendUrlParam(reqMap);
parms = parms.replace("?",""); //xx=v1&xx=v2的参数形式
//头一定要设置,如果不设置,默认也是下面这种编码方式,最好还是设置下。
String[][] header = {{"Content-Type", "application/x-www-form-
urlencoded"}};
String resultJson = HttpClientUtil.post(url,parms ,header,null,null,null);
httpclient代码:
package com.wonders.cop.utils;
import lombok.Data;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Description: httpClient工具类
*
* @author JourWon
* @date Created on 2018年4月19日
*/
public class HttpClientUtils {
/**
* Description: 封装httpClient响应结果
*
* @author JourWon
* @date Created on 2018年4月19日
*/
@Data
public static class HttpClientResult implements Serializable {
/**
* 响应状态码
*/
public int code;
/**
* 响应数据
*/
public String content;
public HttpClientResult(int code, String content) {
this.code = code;
this.content = content;
}
public HttpClientResult(int code) {
this.code = code;
}
}
// 编码格式。发送编码格式统一用UTF-8
private static final String ENCODING = "UTF-8";
// 设置连接超时时间,单位毫秒。
private static final int CONNECT_TIMEOUT = 6000;
// 请求获取数据的超时时间(即响应时间),单位毫秒。
private static final int SOCKET_TIMEOUT = 6000;
/**
* 发送get请求;不带请求头和请求参数
*
* @param url 请求地址
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url) throws Exception {
return doGet(url, null, null);
}
/**
* 发送get请求;带请求参数
*
* @param url 请求地址
* @param params 请求参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url, Map<String, String> params) throws Exception {
return doGet(url, null, params);
}
/**
* 发送get请求;带请求头和请求参数
*
* @param url 请求地址
* @param headers 请求头集合
* @param params 请求参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
// 创建访问的地址
URIBuilder uriBuilder = new URIBuilder(url);
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
uriBuilder.setParameter(entry.getKey(), entry.getValue());
}
}
// 创建http对象
HttpGet httpGet = new HttpGet(uriBuilder.build());
/**
* setConnectTimeout:设置连接超时时间,单位毫秒。
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpGet.setConfig(requestConfig);
// 设置请求头
packageHeader(headers, httpGet);
// 创建httpResponse对象
CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果
return getHttpClientResult(httpResponse, null, httpGet);
} finally {
// 释放资源
release(httpResponse, null);
}
}
/**
* 发送post请求;不带请求头和请求参数
*
* @param url 请求地址
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url) throws Exception {
return doPost(url, null, null);
}
/**
* 发送post请求;带请求参数
*
* @param url 请求地址
* @param params 参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url, Map<String, String> params) throws Exception {
return doPost(url, null, params);
}
/**
* 发送post请求;带请求头和请求参数
*
* @param url 请求地址
* @param headers 请求头集合
* @param params 请求参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
// 创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建http对象
HttpPost httpPost = new HttpPost(url);
/**
* setConnectTimeout:设置连接超时时间,单位毫秒。
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
*/
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig);
// 设置请求头
/*httpPost.setHeader("Cookie", "");
httpPost.setHeader("Connection", "keep-alive");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");*/
packageHeader(headers, httpPost);
// 封装请求参数
packageParam(params, httpPost);
// 创建httpResponse对象
CloseableHttpResponse httpResponse = null;
try {
// 执行请求并获得响应结果
return getHttpClientResult(httpResponse, httpClient, httpPost);
} finally {
// 释放资源
release(httpResponse, httpClient);
}
}
/**
* 发送put请求;不带请求参数
*
* @param url 请求地址
* @return
* @throws Exception
*/
public static HttpClientResult doPut(String url) throws Exception {
return doPut(url);
}
/**
* 发送put请求;带请求参数
*
* @param url 请求地址
* @param params 参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doPut(String url, Map<String, String> params) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPut.setConfig(requestConfig);
packageParam(params, httpPut);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse, httpClient, httpPut);
} finally {
release(httpResponse, httpClient);
}
}
/**
* 发送delete请求;不带请求参数
*
* @param url 请求地址
* @return
* @throws Exception
*/
public static HttpClientResult doDelete(String url) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpDelete.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
try {
return getHttpClientResult(httpResponse, httpClient, httpDelete);
} finally {
release(httpResponse, httpClient);
}
}
/**
* 发送delete请求;带请求参数
*
* @param url 请求地址
* @param params 参数集合
* @return
* @throws Exception
*/
public static HttpClientResult doDelete(String url, Map<String, String> params) throws Exception {
if (params == null) {
params = new HashMap<String, String>();
}
params.put("_method", "delete");
return doPost(url, params);
}
/**
* Description: 封装请求头
* @param params
* @param httpMethod
*/
public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
// 封装请求头
if (params != null) {
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
// 设置到请求头到HttpRequestBase对象中
httpMethod.setHeader(entry.getKey(), entry.getValue());
}
}
}
/**
* Description: 封装请求参数
*
* @param params
* @param httpMethod
* @throws UnsupportedEncodingException
*/
public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
throws UnsupportedEncodingException {
// 封装请求参数
if (params != null) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> entrySet = params.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
// 设置到请求的http对象中
httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
}
}
/**
* Description: 获得响应结果
*
* @param httpResponse
* @param httpClient
* @param httpMethod
* @return
* @throws Exception
*/
public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
if(httpClient==null) {
HttpClientContext context = HttpClientContext.create();
HttpClientConnectionManager connMrg = new BasicHttpClientConnectionManager();
HttpRoute route = new HttpRoute(new HttpHost("www.yangzhou.gov.cn"));
ConnectionRequest connRequest = connMrg.requestConnection(route, null);
// Wait for connection up to 10 sec
HttpClientConnection conn = connRequest.get(1000, TimeUnit.SECONDS);
// If not open
if (!conn.isOpen()) {
// establish connection based on its route info
connMrg.connect(conn, route, 1000, context);
// and mark it as route complete
connMrg.routeComplete(conn, route, context);
}
// 创建httpClient对象
httpClient = HttpClients.custom().setConnectionManager(connMrg).build();
}
// 执行请求
httpResponse = httpClient.execute(httpMethod);
// 获取返回结果
if (httpResponse != null && httpResponse.getStatusLine() != null) {
String content = "";
if (httpResponse.getEntity() != null) {
content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
}
return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
}
return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
/**
* Description: 释放资源
*
* @param httpResponse
* @param httpClient
* @throws IOException
*/
public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
// 释放资源
if (httpResponse != null) {
httpResponse.close();
}
if (httpClient != null) {
httpClient.close();
}
}
/**
* POST请求示例
*
* @author 小明
*
*/
public static void postForm(String url1,String parma) {
try {
// 1. 获取访问地址URL
URL url = new URL(url1);
// 2. 创建HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
/* 3. 设置请求参数等 */
// 请求方式
connection.setRequestMethod("POST");
// 超时时间
connection.setConnectTimeout(3000);
// 设置是否输出
connection.setDoOutput(true);
// 设置是否读入
connection.setDoInput(true);
// 设置是否使用缓存
connection.setUseCaches(false);
// 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
connection.setInstanceFollowRedirects(true);
// 设置使用标准编码格式编码参数的名-值对
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 连接
connection.connect();
/* 4. 处理输入输出 */
// 写入参数到请求中
// String params = "username=test&password=123456";
String params = parma;
OutputStream out = connection.getOutputStream();
out.write(params.getBytes());
out.flush();
out.close();
// 从连接中读取响应信息
String msg = "";
int code = connection.getResponseCode();
if (code == 200) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
msg += line + "\n";
}
reader.close();
}
// 5. 断开连接
connection.disconnect();
// 处理结果
System.out.println(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
参考:
https://blog.youkuaiyun.com/u012838207/article/details/82867701
https://www.jianshu.com/p/9504ecc7abad
很好:https://www.jianshu.com/p/53b5bd0f1d44