色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Java利用httpclient通過get、post方式調(diào)用https接口的方法

瀏覽:178日期:2022-08-17 09:42:52

通過httpclient的get post方式調(diào)用http很常見。一般都是

HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(http://127.0.0.1/login);

但是如果要調(diào)用https這個(gè)方式就不行了。就要修改DefaultHttpClient

<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version></dependency>先導(dǎo)入包

然后重寫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;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)); }}

這時(shí)候就可以使用https方式調(diào)用了

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.HttpGet;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 doGet(String url,String charset) throws Exception{ HttpClient httpClient = null; HttpGet Httpget = null; String result = null; httpClient = new SSLClient(); Httpget = new HttpGet(url); Httpget.addHeader('Content-Type', 'application/json'); HttpGet.setEntity(se); HttpResponse response = httpClient.execute(Httpget); if(response != null){ HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,charset); } } return result; } public static String doPost(String url,String json,String charset) throws Exception{ HttpClient httpClient = null; HttpPost HttpPost = null; String result = null; httpClient = new SSLClient(); HttpPost = new HttpPost(url); HttpPost.addHeader('Content-Type', 'application/json'); StringEntity se = new StringEntity(json); 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); } } return result; }}

post調(diào)用代碼

public static void main(String[] args) throws Exception{ String url = 'https://127.0.0.1/getuser'; String json = '{'id':1}'; String str = HttpClientUtil.doPost(url, json, 'utf-8'); System.out.println(str); }

get調(diào)用代碼

public static void main(String[] args) throws Exception{ String url = 'https://127.0.0.1/getuser?id=1'; String str = HttpClientUtil.doPost(url, 'utf-8'); System.out.println(str); }

StringEntity參數(shù)說明se.setContentEncoding(new BasicHeader(“Content-Type”, “application/json”));使用的是json模式 所以傳的格式是json

application/xhtml+xml :XHTML格式application/xml : XML數(shù)據(jù)格式application/atom+xml :Atom XML聚合格式application/json : JSON數(shù)據(jù)格式application/pdf :pdf格式application/msword : Word文檔格式application/octet-stream : 二進(jìn)制流數(shù)據(jù)(如常見的文件下載)application/x-www-form-urlencoded : 中默認(rèn)的encType,form表單數(shù)據(jù)被編碼為key/value格式發(fā)送到服務(wù)器(表單默認(rèn)的提交數(shù)據(jù)的格式)

HttpPost.addHeader('Content-Type', ' application/x-www-form-urlencoded');List<NameValuePair> params=new ArrayList<>();params.add(new BasicNameValuePair('key1','value1'));params.add(new BasicNameValuePair('key2','value2'));params.add(new BasicNameValuePair('key3','value3'));UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,'UTF-8');HttpPost.setEntity(entity);

如果要采用表單提交方式就需要修改成上面所描述的方式。

到此這篇關(guān)于Java利用httpclient通過get、post方式調(diào)用https接口的文章就介紹到這了,更多相關(guān)java調(diào)用https接口內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 2022久久免费精品国产72精品 | 国产成人精品免费视频大 | 亚洲在线免费 | 国内精品久久影视 | 99久久精品久久久久久婷婷 | chinese性老妇中国 | 国产精品久久在线 | 国产毛片精品 | 一区二区三区 亚洲区 | 国产一级一片免费播放刺激 | 国产精品19p| 丝袜美腿精品一区二区三 | 韩国视频一区 | 欧美中日韩在线 | 亚洲成人一级片 | 欧美精品videosbestsex另类 | 99久久99久久精品免费看子伦 | 在线视频久久 | 99免费视频观看 | 国产精品夜色视频一区二区 | 亚洲经典三级 | 一级毛片在线观看视频 | 国产美女精品三级在线观看 | 成年人在线视频免费观看 | 中文字幕在线视频观看 | 国产的一级毛片完整 | 亚洲精品国产综合99久久一区 | 美女很黄很黄 | 欧美日韩成人 | 成人免费看毛片 | 毛片免费在线视频 | 色www永久免费 | 波多野结衣免费视频观看 | 热伊人99re久久精品最新地 | 久久精品国产免费观看99 | 日韩中文字幕一在线 | 欧美精品久久一区二区三区 | 久久99国产精品亚洲 | 成人做爰毛片免费视频 | 无内丝袜透明在线播放 | 久久九九国产 |