Java利用httpclient通过get、post方式调用https接口的方法

时间:2022-02-06 03:05:30

通过httpclient的get post方式调用http很常见。一般都是

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

但是如果要调用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>

先导入包

然后重写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方式调用了

?
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调用代码

?
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调用代码

?
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参数说明
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格式发送到服务器(表单默认的提交数据的格式)

?
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);

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

到此这篇关于java利用httpclient通过get、post方式调用https接口的文章就介绍到这了,更多相关java调用https接口内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

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