httpClient发送 返回报文乱码,返回的报文确实为utf-8,然而用
HttpEntity entity = response.getEntity();
String retule=EntityUtils.toString(entity, "UTF-8");
指定实体的编码返回的报文还是乱码。
解决办法---------------
①:
HttpEntity entity = response.getEntity()
InputStream is =entity.getContent();
用输出流接收is中的内容就行了,这样的话第三方放回什么内容,此处就会接收到什么内容,不会出现乱码的问题。
②:
使用String类的构造方法进行编码转换
String temp=EntityUtils.toString(entity, "UTF-8");
String result=new String(temp.getBytes("ISO-8859-1"),"utf-8")。
public static String sendSSLPostRequest_unitedB2C(String reqURL,
Map<String, String> params) {
String responseContent = null; // 响应内容
HttpClient httpClient = new DefaultHttpClient(); // 创建默认的httpClient实例
X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
try {
// TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
SSLContext ctx = SSLContext.getInstance("TLS");
// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
ctx.init(null, new TrustManager[] { xtm }, null);
// 创建SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
// 通过SchemeRegistry将SSLSocketFactory注册到我们的HttpClient上
httpClient.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", 443, socketFactory));
// 请求超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
// 读取超时
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 20000);
HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost
List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 构建POST请求的表单参数
for (Map.Entry<String, String> entry : params.entrySet()) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost); // 执行POST请求
HttpEntity entity = response.getEntity(); // 获取响应实体
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (null != entity) {
InputStream is = entity.getContent();
int len=0;
byte[] b=new byte[2014];
while((len = is.read(b)) != -1){
baos.write(b, 0, len);
}
}
responseContent=new String(baos.toByteArray());
} catch (Exception e) {
logger.error("HttpClientUtil SSL POST请求异常,堆栈信息如下", e);
} finally {
httpClient.getConnectionManager().shutdown(); // 关闭连接,释放资源
}
return responseContent;
}
相关推荐
采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。
不会的可以在评论区留言哈,这是我自己做项目用到的。所以绝对可用!同时共享出来给到大家
try(CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(params, Charset.forName("UTF-8")); ...
利用HttpClient发送HTTP请求 利用HttpClient发送HTTP请求
其中包含项目post-demo定义接口;包含另一个项目httpclient-demo使用工具类发送post请求调用接口。详细内容查看博客 httpclient发送post请求
java HttpClient 发送GET请求和带有表单参数的POST请求教程例子
一个简单的易学的 基于HttpClient 4.3发送psot及get请求,返回数据,适合初学者,适合初学者
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
解决证书错误,利用httpClient发送https请求,内容格式为XML,另外包中有sha1加密,需要的包,速速下载
httpclient的用法,发送get请求和post请求,设置header
httpclient发送post请求
本代码是httpclient发送get请求和post请求demo,具体描述参考博客 HttpClient发送get请求和post请求
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解
httpclient post方式发送json格式数据,需要的相关包。
HttpClientUtil工具类发送get和post请求,支持http和https,支持发送文件
HttpClient发送http请求(post+get)需要的jar包+内符java代码案例+注解详解(这款工具类是我精心挑选适合广大java程序猿使用)
使用HttpClient发送http请求需要的jar包。org.apache.http的jar包
NULL 博文链接:https://daoshud1.iteye.com/blog/1847378