JAVA利用HttpClient进行HTTPS接口调用的方法
程序员文章站
2024-02-23 21:39:16
本文介绍了java利用httpclient进行https接口调用的方法,分享给大家,具体如下:
1.为了避免需要证书,所以用一个类继承defaulthttpclien...
本文介绍了java利用httpclient进行https接口调用的方法,分享给大家,具体如下:
1.为了避免需要证书,所以用一个类继承defaulthttpclient类,忽略校验过程。
import java.security.cert.certificateexception; import java.security.cert.x509certificate; import javax.net.ssl.sslcontext; import javax.net.ssl.trustmanager; import javax.net.ssl.x509trustmanager; import org.apache.http.conn.clientconnectionmanager; import org.apache.http.conn.scheme.scheme; import org.apache.http.conn.scheme.schemeregistry; import org.apache.http.conn.ssl.sslsocketfactory; import org.apache.http.impl.client.defaulthttpclient; /** * 用于进行https请求的httpclient * @classname: sslclient * @description: todo * @author devin <xxx> * @date 2017年2月7日 下午1:42:07 * */ 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)); } }
2.创建一个利用httpclient发送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; /** * 利用httpclient进行post请求的工具类 * @classname: httpclientutil * @description: todo * @author devin <xxx> * @date 2017年2月7日 下午1:43:38 * */ public class httpclientutil { @suppresswarnings("resource") public static string dopost(string url,string jsonstr,string charset){ httpclient httpclient = null; httppost httppost = null; string result = null; try{ httpclient = new sslclient(); 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; } }
3.测试代码
public static void main(string[] args){ string url = "https://192.168.1.101/xxx"; string jsonstr = "{xxx}"; string httporgcreatetestrtn = httpclientutil.dopost(url, jsonstr, "utf-8"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: response对象的使用(实例讲解)