Java利用httpclient通过get、post方式调用https接口的方法
通过httpclient的getpost方式调用http很常见。一般都是
HttpClientclient=newDefaultHttpClient(); HttpPostpost=newHttpPost(http://127.0.0.1/login);
但是如果要调用https这个方式就不行了。就要修改DefaultHttpClient
org.apache.httpcomponents httpclient 4.5.5 com.alibaba fastjson 1.2.47
先导入包
然后重写DefaultHttpClient的类
importjava.security.cert.CertificateException;
importjava.security.cert.X509Certificate;
importjavax.net.ssl.SSLContext;
importjavax.net.ssl.TrustManager;
importjavax.net.ssl.X509TrustManager;
importorg.apache.http.conn.ClientConnectionManager;
importorg.apache.http.conn.scheme.Scheme;
importorg.apache.http.conn.scheme.SchemeRegistry;
importorg.apache.http.conn.ssl.SSLSocketFactory;
importorg.apache.http.impl.client.DefaultHttpClient;
publicclassSSLClientextendsDefaultHttpClient{
publicSSLClient()throwsException{
super();
SSLContextctx=SSLContext.getInstance("TLS");
X509TrustManagertm=newX509TrustManager(){
@Override
publicvoidcheckClientTrusted(X509Certificate[]chain,
StringauthType)throwsCertificateException{
}
@Override
publicvoidcheckServerTrusted(X509Certificate[]chain,
StringauthType)throwsCertificateException{
}
@Override
publicX509Certificate[]getAcceptedIssuers(){
returnnull;
}
};
ctx.init(null,newTrustManager[]{tm},null);
SSLSocketFactoryssf=newSSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManagerccm=this.getConnectionManager();
SchemeRegistrysr=ccm.getSchemeRegistry();
sr.register(newScheme("https",443,ssf));
}
}
这时候就可以使用https方式调用了
importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.StatusLine;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.client.methods.HttpPost;
importorg.apache.http.entity.StringEntity;
importorg.apache.http.message.BasicHeader;
importorg.apache.http.util.EntityUtils;
publicclassHttpClientUtil{
publicstaticStringdoGet(Stringurl,Stringcharset)throwsException{
HttpClienthttpClient=null;
HttpGetHttpget=null;
Stringresult=null;
httpClient=newSSLClient();
Httpget=newHttpGet(url);
Httpget.addHeader("Content-Type","application/json");
HttpGet.setEntity(se);
HttpResponseresponse=httpClient.execute(Httpget);
if(response!=null){
HttpEntityresEntity=response.getEntity();
if(resEntity!=null){
result=EntityUtils.toString(resEntity,charset);
}
}
returnresult;
}
publicstaticStringdoPost(Stringurl,Stringjson,Stringcharset)throwsException{
HttpClienthttpClient=null;
HttpPostHttpPost=null;
Stringresult=null;
httpClient=newSSLClient();
HttpPost=newHttpPost(url);
HttpPost.addHeader("Content-Type","application/json");
StringEntityse=newStringEntity(json);
se.setContentType("text/json");
se.setContentEncoding(newBasicHeader("Content-Type","application/json"));
HttpPost.setEntity(se);
HttpResponseresponse=httpClient.execute(HttpPost);
if(response!=null){
HttpEntityresEntity=response.getEntity();
if(resEntity!=null){
result=EntityUtils.toString(resEntity,charset);
}
}
returnresult;
}
}
post调用代码
publicstaticvoidmain(String[]args)throwsException{
Stringurl="https://127.0.0.1/getuser";
Stringjson="{\"id\":1}";
Stringstr=HttpClientUtil.doPost(url,json,"utf-8");
System.out.println(str);
}
get调用代码
publicstaticvoidmain(String[]args)throwsException{
Stringurl="https://127.0.0.1/getuser?id=1";
Stringstr=HttpClientUtil.doPost(url,"utf-8");
System.out.println(str);
}
StringEntity参数说明
se.setContentEncoding(newBasicHeader(“Content-Type”,“application/json”));
使用的是json模式所以传的格式是json
application/xhtml+xml:XHTML格式
application/xml:XML数据格式
application/atom+xml:AtomXML聚合格式
application/json:JSON数据格式
application/pdf:pdf格式
application/msword:Word文档格式
application/octet-stream:二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded:中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
HttpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
Listparams=newArrayList<>();
params.add(newBasicNameValuePair("key1","value1"));
params.add(newBasicNameValuePair("key2","value2"));
params.add(newBasicNameValuePair("key3","value3"));
UrlEncodedFormEntityentity=newUrlEncodedFormEntity(params,"UTF-8");
HttpPost.setEntity(entity);
如果要采用表单提交方式就需要修改成上面所描述的方式。
到此这篇关于Java利用httpclient通过get、post方式调用https接口的文章就介绍到这了,更多相关java调用https接口内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。