欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-05 10:15:00
通过httpclient的get post方式调用http很常见。一般都是httpclient client = new defaulthttpclient(); httppost post = ne...

通过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 https 接口