一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - Java利用httpclient通過get、post方式調(diào)用https接口的方法

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

2021-08-04 11:12godliu711 Java教程

這篇文章主要介紹了Java利用httpclient通過get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

?
1
2
httpclient client = new defaulthttpclient();
httppost post = new httppost(http://127.0.0.1/login);

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

?
1
2
3
4
5
6
7
8
9
10
11
<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的類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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方式調(diào)用了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)用代碼

?
1
2
3
4
5
6
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)用代碼

?
1
2
3
4
5
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 : 二進制流數(shù)據(jù)(如常見的文件下載)
application/x-www-form-urlencoded : 中默認的enctype,form表單數(shù)據(jù)被編碼為key/value格式發(fā)送到服務(wù)器(表單默認的提交數(shù)據(jù)的格式)

?
1
2
3
4
5
6
7
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ù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/terry711/article/details/112471780

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年女人毛片免费观看中文w | 男男视频18免费网站 | 91精品手机国产在线观 | 好吊日在线 | 国产精品毛片久久久久久久 | xxxxxx国产精品视频 | 日韩色在线观看 | 动漫美女人物被黄漫小说 | 啪啪免费入口网站 | 9191精品国产观看 | 免费国产成人高清视频网站 | 日本在线观看www鲁啊鲁视频 | 国产高清在线精品一区二区 | 亚洲第一综合网 | 国产福利在线观看第二区 | 欧美日韩一区二区三区免费 | 999热在线精品观看全部 | 国产免费好大好硬视频 | 特黄特色大片免费影院 | 美女被躁了在线观看视频 | 欧美一区二区三区四区视频 | 国产馆在线观看免费的 | 欧美久久综合网 | 国产露脸对白刺激3p在线 | 国产精品国产精品国产三级普 | 亚洲H成年动漫在线观看不卡 | 2018高清国产一道国产 | 操美女骚b| 精品国产品国语在线不卡丶 | 天天爽视频 | 草嫩社区| 97午夜 | 国产精品久久国产三级国电话系列 | 日本人泡妞18xxⅹ | 亚洲性色永久网址 | 国产91精品在线播放 | 女子监狱第二季在线观看免费完整版 | 无套内射在线观看THEPORN | 欧美亚洲国产精品久久第一页 | 99久久精品国产免看国产一区 | 把内裤拔到一边高h1v1 |