最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年02月06日 11:26:41   作者:godliu711  
這篇文章主要介紹了Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

通過(guò)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ù)說(shuō)明
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通過(guò)get、post方式調(diào)用https接口的文章就介紹到這了,更多相關(guān)java調(diào)用https接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java變量的聲明與賦值分離規(guī)范示例

    java變量的聲明與賦值分離規(guī)范示例

    這篇文章主要為大家介紹了java變量的聲明與賦值分離規(guī)范示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Java環(huán)境配置原理全面解析

    Java環(huán)境配置原理全面解析

    下面小編就為大家?guī)?lái)一篇Java環(huán)境配置原理全面解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • 解決bootstrap.yml不生效,無(wú)法優(yōu)先于application.yml文件加載問題

    解決bootstrap.yml不生效,無(wú)法優(yōu)先于application.yml文件加載問題

    文章主要討論了在Spring Boot項(xiàng)目中,`bootstrap.yml`文件無(wú)法優(yōu)先于`application.yml`文件加載的問題,原因是缺少了`nacos-config`依賴,且需要確保Spring Boot版本與`nacos-config`版本匹配,作者希望通過(guò)分享個(gè)人經(jīng)驗(yàn),幫助他人解決類似問題
    2024-12-12
  • SpringBoot啟動(dòng)時(shí)如何修改上下文

    SpringBoot啟動(dòng)時(shí)如何修改上下文

    本文介紹了如何在Spring Boot啟動(dòng)時(shí)修改上下文,以便加載封裝JAR中的國(guó)際化文件,通過(guò)在resources目錄下的META-INF文件夾中的spring.factories文件中配置指定類,可以實(shí)現(xiàn)這一功能
    2024-11-11
  • springboot多租戶設(shè)計(jì)過(guò)程圖解

    springboot多租戶設(shè)計(jì)過(guò)程圖解

    這篇文章主要介紹了springboot多租戶設(shè)計(jì)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java并發(fā)編程之同步容器

    Java并發(fā)編程之同步容器

    這篇文章主要介紹了Java并發(fā)編程之同步容器,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • maven install報(bào)錯(cuò)中程序包xxx不存在的問題解決

    maven install報(bào)錯(cuò)中程序包xxx不存在的問題解決

    本文主要介紹了maven install報(bào)錯(cuò)中程序包xxx不存在的問題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能

    springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能

    mica-mqtt-client-spring-boot-starter是一個(gè)方便、高效、可靠的MQTT客戶端啟動(dòng)器,適用于需要使用MQTT協(xié)議進(jìn)行消息通信的Spring Boot應(yīng)用程序,這篇文章主要介紹了springboot整合mqtt實(shí)現(xiàn)消息訂閱和推送功能,需要的朋友可以參考下
    2024-02-02
  • 詳解Maven optional關(guān)鍵字透徹圖解

    詳解Maven optional關(guān)鍵字透徹圖解

    這篇文章主要介紹了詳解Maven optional關(guān)鍵字透徹圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java使用Soap方式調(diào)用WebService接口代碼示例

    Java使用Soap方式調(diào)用WebService接口代碼示例

    Java調(diào)用WebService接口是指通過(guò)Java語(yǔ)言來(lái)訪問并與WebService進(jìn)行交互,WebService是一種基于Web的服務(wù)架構(gòu),它通過(guò)標(biāo)準(zhǔn)的XML和HTTP協(xié)議來(lái)提供服務(wù),這篇文章主要給大家介紹了關(guān)于Java使用Soap方式調(diào)用WebService接口的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評(píng)論

益阳市| 合山市| 武夷山市| 峡江县| 无为县| 日土县| 津市市| 黄大仙区| 澄城县| 五大连池市| 宜宾市| 巴彦县| 万年县| 泸定县| 紫阳县| 麻阳| 东乌| 渭南市| 阳信县| 河南省| 济源市| 井冈山市| 得荣县| 新乡县| 海安县| 翁牛特旗| 鹤岗市| 同心县| 上饶县| 木里| 宁远县| 乾安县| 佛坪县| 津市市| 元氏县| 肥西县| 双柏县| 锡林浩特市| 体育| 汝南县| 小金县|