1,出现场景:微信回调函数访问后发送模板消息,之前项目中使用的http的post请求没有问题,然后复制在新项目中,就报org.apache.http.NoHttpResponseException错误,后来在网上搜索了一大堆也没有解决问题,后面就调用其他post请求的数据类型的方式(xml,map,json)但是还是报错
2,后面就不断修改代码不断测试,直到将Content-Type的类型修改成utf-8,然后就正常了,这是什么鬼哟,Content-Type是没有这个类型的吧???具体原因还没有找到,但是可正常请求返回了
3,请求截图
4,请求结果:
5,http工具类
package com.pflm.util.httpclient;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import com.alibaba.fastjson.JSONObject;
public class HttpClientUtils {
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
public static final ContentType UTF8_TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", Consts.UTF_8);
public static final ContentType DEFAULT_BINARY_CONTENT_TYPE = ContentType.create("application/octet-stream",
Consts.UTF_8);
private static final String ACCEPT_LANGUAGE = "zh-CN,zh;q=0.8";
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
//private static final String JSON_CONTENT_TYPE = "application/json; encoding=utf-8";
/**
* 修改成utf-8,就可正常请求了
*/
private static final String JSON_CONTENT_TYPE = "utf-8";
private static final int TIME_OUT = 10 * 1000;
private static CloseableHttpClient httpClient = null;
public static CloseableHttpClient getHttpClient() {
if (httpClient == null) {
try {
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// Honor 'keep-alive' header
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
HttpHost target = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST);
if ("file.api.weixin.qq.com".equalsIgnoreCase(target.getHostName())) {
// Keep alive for 5 seconds only
return 10 * 1000;
} else {
// otherwise keep alive for 30 seconds
return 30 * 1000;
}
}
};
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 3) {
// 如果已经重试了3次,就放弃
return false;
}
if (exception instanceof InterruptedIOException) {
// 超时
return false;
}
if (exception instanceof UnknownHostException) {
// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {
// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {
// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// 如果请求是幂等的,就再次尝试
return true;
}
return false;
}
};
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
// 将最大连接数增加到200
connManager.setMaxTotal(500);
// 将每个路由基础的连接增加到20
connManager.setDefaultMaxPerRoute(100);
// 将目标主机的最大连接数增加到100
HttpHost weiXinApiHost = new HttpHost("api.weixin.qq.com", 443);
connManager.setMaxPerRoute(new HttpRoute(weiXinApiHost), 200);
// Create socket configuration
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
connManager.setDefaultSocketConfig(socketConfig);
// Create connection configuration
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
connManager.setDefaultConnectionConfig(connectionConfig);
// Use custom cookie store if necessary.
// CookieStore cookieStore = new BasicCookieStore();
// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom()
// .setCookieSpec(CookieSpecs.BEST_MATCH)
.setExpectContinueEnabled(false).setStaleConnectionCheckEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.setConnectionRequestTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).setSocketTimeout(TIME_OUT)
.build();
HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
request.addHeader(HttpHeaders.ACCEPT_LANGUAGE, ACCEPT_LANGUAGE);
// request.addHeader(HttpHeaders.USER_AGENT,
// USER_AGENT);
}
};
HttpResponseInterceptor gizpResponseInterceptor = new HttpResponseInterceptor() {
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
};
httpClient = HttpClients.custom().setConnectionManager(connManager).setKeepAliveStrategy(myStrategy)
// .setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(defaultRequestConfig).setRetryHandler(myRetryHandler)
.addInterceptorFirst(requestInterceptor).addInterceptorFirst(gizpResponseInterceptor).build();
} catch (KeyManagementException e) {
log.error(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
} catch (KeyStoreException e) {
log.error(e.getMessage(), e);
}
}
return httpClient;
}
/**
* GET提交数据,并返回JSON格式的结果数据
*
* @param url
* 请求URL
* @return JSONObject or null if error or no response
*/
public static JSONObject getForJsonResult(String reqUrl) {
return getForJsonResult(reqUrl, null);
}
/**
* GET提交数据,并返回JSON格式的结果数据
*
* @param url
* 请求URL
* @param 请求参数MAP
* @return JSONObject or null if error or no response
*/
public static JSONObject getForJsonResult(String reqUrl, Map<String, String> params) {
try {
if (params != null) {
reqUrl = buildReqUrl(reqUrl, params);
}
return getHttpClient().execute(new HttpGet(reqUrl), new JsonResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* GET提交并返回XML数据
*
* @param url
* 请求URL
* @return org.w3c.dom.Document or null
*/
public static Document getForXmlResult(String reqUrl) {
return getForXmlResult(reqUrl, null);
}
/**
* GET提交并返回XML数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return org.w3c.dom.Document or null
*/
public static Document getForXmlResult(String reqUrl, Map<String, String> params) {
try {
if (params != null) {
reqUrl = buildReqUrl(reqUrl, params);
}
return getHttpClient().execute(new HttpGet(reqUrl), new XmlResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* GET提交并返回String数据
*
* @param url
* 请求URL
* @return String or null
*/
public static String getForStringResult(String reqUrl) {
return getForStringResult(reqUrl, null);
}
/**
* GET提交并返回String数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return String or null
*/
public static String getForStringResult(String reqUrl, Map<String, String> params) {
try {
if (params != null) {
reqUrl = buildReqUrl(reqUrl, params);
}
return getHttpClient().execute(new HttpGet(reqUrl), new StringResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交数据,并返回JSON格式的结果数据
*
* @param url
* 请求URL
* @return JSONObject or null if error or no response
*/
public static JSONObject postForJsonResult(String url) {
return postForJsonResult(url, null);
}
/**
* POST提交数据,并返回JSON格式的结果数据
*
* @param url
* 请求URL
* @param 请求参数MAP
* @return JSONObject or null if error or no response
*/
public static JSONObject postForJsonResult(String url, Map<String, String> params) {
try {
HttpPost post = new HttpPost(url);
if (params != null) {
post.setEntity(buildUrlEncodedFormEntity(params));
}
return getHttpClient().execute(post, new JsonResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交JSON数据,并返回JSON格式的结果数据
*
* @param url
* 请求URL
* @param jsonDataStr
* jsonData String
* @return JSONObject or null if error or no response
*/
public static JSONObject postJsonDataForJsonResult(String url, String jsonDataStr) {
System.err.println("222222");
try {
HttpPost post = new HttpPost(url);
if (jsonDataStr != null) {
StringEntity reqEntity = new StringEntity(jsonDataStr, Consts.UTF_8);
reqEntity.setContentType(JSON_CONTENT_TYPE);
post.setEntity(reqEntity);
}
return getHttpClient().execute(post, new JsonResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交并返回XML数据
*
* @param url
* 请求URL
* @return org.w3c.dom.Document or null
*/
public static Document postForXmlResult(String url) {
return postForXmlResult(url, null, "utf-8");
}
/**
* POST提交并返回XML数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return org.w3c.dom.Document or null
*/
public static Document postForXmlResult(String url, Map<String, String> params) {
try {
HttpPost post = new HttpPost(url);
if (params != null) {
post.setEntity(buildUrlEncodedFormEntity(params));
}
return getHttpClient().execute(post, new XmlResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交并返回XML数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return org.w3c.dom.Document or null
*/
public static Document postForXmlResult(String url, Map<String, String> params, String charset) {
try {
HttpPost post = new HttpPost(url);
if (params != null) {
post.setEntity(buildUrlEncodedFormEntity(params, charset));
}
return getHttpClient().execute(post, new XmlResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交并返回String数据
*
* @param url
* 请求URL
* @return String or null
*/
public static String postForStringResult(String url) {
return postForStringResult(url, null);
}
/**
* POST提交并返回String数据
*
* @param url
* 请求URL
* @return String or null
*/
public static String postXmlForStringResult(String url, String xml, String charset) {
try {
HttpPost post = new HttpPost(url);
if (charset == null) {
charset = "utf-8";
}
if (xml != null) {
StringEntity myEntity = new StringEntity(xml, charset);
myEntity.setContentType("application/xml;charset=" + charset);
// post.addHeader("Content-Type", "text/xml");
post.setEntity(myEntity);
}
return getHttpClient().execute(post, new StringResponseHandler(charset));
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
e.printStackTrace();
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交并返回String数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return String or null
*/
public static String postForStringResult(String url, Map<String, String> params) {
try {
HttpPost post = new HttpPost(url);
if (params != null) {
post.setEntity(buildUrlEncodedFormEntity(params));
}
return getHttpClient().execute(post, new StringResponseHandler());
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* POST提交并返回String数据
*
* @param url
* 请求URL
* @param params
* 请求参数MAP
* @return String or null
*/
public static String postForStringResult(String url, Map<String, String> params, String charset) {
try {
HttpPost post = new HttpPost(url);
if (params != null) {
post.setEntity(buildUrlEncodedFormEntity(params, charset));
}
return getHttpClient().execute(post, new StringResponseHandler(charset));
} catch (ClientProtocolException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.warn(e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
/**
* 上传微信媒体文件
*
* @param url
* @param file
* @return
*/
public static String postWeiXinMedia(String url, File file) {
return postMultipartForm(url, "media", file);
}
/**
* 文件上传
*
* @param url
* @param file
* @return
*/
public static String postMultipartForm(String url, File file) {
return postMultipartForm(url, "upFile", file);
}
/**
* 文件上传
*
* @param url
* @param keyName
* @param file
* @return
*/
public static String postMultipartForm(String url, String keyName, File file) {
return postMultipartForm(url, keyName, file, null);
}
/**
* 文件上传
*
* @param url
* @param keyName
* @param file
* @param fileName
* @return
*/
public static String postMultipartForm(String url, String keyName, File file, String fileName) {
return postMultipartForm(url, keyName, file, fileName, null);
}
/**
* 文件上传(使用表单)
*
* @param url
* 请求URL
* @param keyName
* 上传表单的文件域名称
* @param file
* 要上传的文件
* @param fileName
* 文件名
* @param params
* 请求参数MAP
* @return String or null
*/
public static String postMultipartForm(String url, String keyName, File file, String fileName,
Map<String, String> params) {
if (file == null || !file.isFile() || !file.canRead()) {
log.error("File is null or file can't read!");
return null;
}
if (keyName == null) {
log.error("keyName is null!");
return null;
}
try {
HttpPost httpPost = new HttpPost(url);
if (fileName == null)
fileName = file.getName();
FileBody bin = new FileBody(file, DEFAULT_BINARY_CONTENT_TYPE, fileName);
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.addPart(keyName, bin);
// reqEntity.addPart("filename", new StringBody(file.getName(),
// UTF8_TEXT_PLAIN_CONTENT_TYPE));
// reqEntity.addTextBody("filelength", file.length()+"");
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
String value = entry.getValue();
if (value != null)
reqEntity.addPart(entry.getKey(), new StringBody(value, UTF8_TEXT_PLAIN_CONTENT_TYPE));
}
}
httpPost.setEntity(reqEntity.build());
CloseableHttpResponse response = getHttpClient().execute(httpPost);
try {
HttpEntity resEntity = response.getEntity();
String resp = null;
if (resEntity != null) {
resp = EntityUtils.toString(resEntity, Consts.UTF_8);
}
EntityUtils.consume(resEntity);
return resp;
} finally {
response.close();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
public static String buildReqUrl(String reqUrl, Map<String, String> params) {
if (reqUrl == null || params == null)
return null;
String[] reqUrls = reqUrl.split("\\?");
StringBuilder sp = new StringBuilder();
sp.append(reqUrls[0]).append("?");
List<NameValuePair> parameters = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, String> entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
sp.append(URLEncodedUtils.format(parameters, Consts.UTF_8));
return sp.toString();
}
public static UrlEncodedFormEntity buildUrlEncodedFormEntity(Map<String, String> params)
throws ClientProtocolException, UnsupportedEncodingException {
return buildUrlEncodedFormEntity(params, "utf-8");
}
public static UrlEncodedFormEntity buildUrlEncodedFormEntity(Map<String, String> params, String charset)
throws ClientProtocolException, UnsupportedEncodingException {
if (params == null)
throw new ClientProtocolException("Params is null");
List<NameValuePair> parameters = new ArrayList<NameValuePair>(params.size());
for (Map.Entry entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
}
return new UrlEncodedFormEntity(parameters, charset);
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求參数,请求參数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置例如以下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象相应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求參数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常。" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
6,完整的http工具类:https://download.csdn.net/download/baidu_38226233/11228610