我是靠谱客的博主 无心鞋垫,最近开发中收集的这篇文章主要介绍使用httpClient发送https消息写作缘由httpClient类的创建创建post方法调用点参考处,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
写作缘由
网上使用httpClient发送https消息的帖子已经很多,方法各有不同。作者在开发中需要使用java代码实现发送https消息,发现大多方法都报“需要加载证书”的错误,经过陆续的筛选,发现其中一个帖子方法可行,于是搬运到这里,供后续查看,此处非原创,请各位适当查看。
httpClient类的创建
目前诸多方法中,只有DefaultHttpClient 方法可以绕过证书实现发送https消息,代码如下
import org.apache.http.impl.client.DefaultHttpClient;
import javax.net.ssl.SSLContext;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.Scheme;
@SuppressWarnings("deprecation")
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
该方法中继承了 DefaultHttpClient 方法,可以绕过证书,目前是发送https消息的不二之选
创建post方法
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
public static String doPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
//httpClient = HttpClient01.createSSLClientDefault();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
http协议有很多方法,其中用的最多的是post方法,此处举例。
调用点
String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, jsonStr, "utf-8");
url为http地址,jsonStr为json格式字符串
参考处
https://www.cnblogs.com/luchangyou/p/6375166.html
最后
以上就是无心鞋垫为你收集整理的使用httpClient发送https消息写作缘由httpClient类的创建创建post方法调用点参考处的全部内容,希望文章能够帮你解决使用httpClient发送https消息写作缘由httpClient类的创建创建post方法调用点参考处所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复