亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java利用httpclient通過get、post方式調用https接口的方法

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

通過httpclient的get post方式調用http很常見。一般都是

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

但是如果要調用https這個方式就不行了。就要修改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>先導入包

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

這時候就可以使用https方式調用了

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調用代碼

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調用代碼

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參數說明se.setContentEncoding(new BasicHeader(“Content-Type”, “application/json”));使用的是json模式 所以傳的格式是json

application/xhtml+xml :XHTML格式application/xml : XML數據格式application/atom+xml :Atom XML聚合格式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');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);

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

到此這篇關于Java利用httpclient通過get、post方式調用https接口的文章就介紹到這了,更多相關java調用https接口內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 日韩美女一级片 | 国产99在线播放免费 | 成人三级精品视频在线观看 | 那里有毛片看 | 亚洲欧美精品中文字幕 | 一级黄色国产片 | 99亚洲乱人伦精品 | 热er99久久6国产精品免费 | 大尺度人体自拍私拍337p | 亚洲日韩中文字幕天堂不卡 | 青青青在线观看视频免费播放 | 悠悠资源先锋中文站 | 日本毛片大全 | 在线免费观看国产精品 | chinese在线播放91国内 | 欧美激情视频一级视频一级毛片 | 亚洲欧美一区在线 | 99re热久久精品这里都是精品 | 欧美日本韩国国产 | a在线观看免费网址大全 | 久久久久青草大香线综合精品 | 日韩在线精品 | 女人被两根一起进3p在线观看 | 国产精品高清一区二区三区 | 色片免费观看 | 国产日韩欧美在线一二三四 | 久久一区精品 | 一级做a爰片久久毛片唾 | aⅴ在线免费观看 | 一级毛片高清大全免费观看 | 一级特黄a免费大片 | 香蕉视频大全 | 在线观看欧美一区 | 成人性欧美丨区二区三区 | 黄色免费一级 | 在线观看免费视频网站色 | 搡的我好爽视频在线观看 | 欧美亚洲国产成人综合在线 | 91天天操| 免费特黄一级欧美大片 | 国产免费播放一区二区三区 |