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

JAVA利用HttpClient进行POST请求(HTTPS)实例

程序员文章站 2024-03-12 08:21:32
最近,需要对客户的接口做一个包装,然后供自己公司别的系统调用,客户接口是用http url实现的,我想用httpclient包进行请求,同时由于请求的url是https的,...

最近,需要对客户的接口做一个包装,然后供自己公司别的系统调用,客户接口是用http url实现的,我想用httpclient包进行请求,同时由于请求的url是https的,为了避免需要证书,所以用一个类继承defaulthttpclient类,忽略校验过程。

1.写一个sslclient类,继承至httpclient

package com.pcmall.service.sale.miaomore.impl; 
 
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;  
//用于进行https请求的httpclient  
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));  
  }  
}  

2.写一个利用httpclient发送post请求的类

package com.pcmall.service.sale.miaomore.impl; 
 
import java.util.arraylist;  
import java.util.iterator;  
import java.util.list;  
import java.util.map;  
import java.util.map.entry;  
import org.apache.http.httpentity;  
import org.apache.http.httpresponse;  
import org.apache.http.namevaluepair;  
import org.apache.http.client.httpclient;  
import org.apache.http.client.entity.urlencodedformentity;  
import org.apache.http.client.methods.httppost;  
import org.apache.http.message.basicnamevaluepair;  
import org.apache.http.util.entityutils;  
/* 
 * 利用httpclient进行post请求的工具类 
 */  
public class httpclientutil {  
  public string dopost(string url,map<string,string> map,string charset){  
    httpclient httpclient = null;  
    httppost httppost = null;  
    string result = null;  
    try{  
      httpclient = new sslclient();  
      httppost = new httppost(url);  
      //设置参数  
      list<namevaluepair> list = new arraylist<namevaluepair>();  
      iterator iterator = map.entryset().iterator();  
      while(iterator.hasnext()){  
        entry<string,string> elem = (entry<string, string>) iterator.next();  
        list.add(new basicnamevaluepair(elem.getkey(),elem.getvalue()));  
      }  
      if(list.size() > 0){  
        urlencodedformentity entity = new urlencodedformentity(list,charset);  
        httppost.setentity(entity);  
      }  
      httpresponse response = httpclient.execute(httppost);  
      if(response != null){  
        httpentity resentity = response.getentity();  
        if(resentity != null){  
          result = entityutils.tostring(resentity,charset);  
        }  
      }  
    }catch(exception ex){  
      ex.printstacktrace();  
    }  
    return result;  
  }  
}  

3.调用post请求的测试代码

package com.pcmall.service.sale.miaomore.impl; 
 
import java.util.hashmap;  
import java.util.map;  
//对接口进行测试  
public class testmain {  
  private string url = "https://xxx.xxx.xxx/";  
  private string charset = "utf-8";  
  private httpclientutil httpclientutil = null;  
    
  public testmain(){  
    httpclientutil = new httpclientutil();  
  }  
    
  public void test(){  
    string httporgcreatetest = url + "xxx/xxx/delivery";  
    map<string,string> createmap = new hashmap<string,string>();  
    createmap.put("delivery_code","1d1qz222z22sm21a");  
    createmap.put("timestamp","1479198840000");  
    createmap.put("sign","f2109c333f3eade929f932e89703fa0f683d43eb");  
    string httporgcreatetestrtn = httpclientutil.dopost(httporgcreatetest,createmap,charset);  
    system.out.println("result:"+httporgcreatetestrtn);  
  }  
    
  public static void main(string[] args){  
    testmain main = new testmain();  
    main.test();  
  }  
}  

刚开始不是很明白basicnamevaluepair的用法,后来慢慢摸索了一下,发现basicnamevaluepair是存储键值对的类,当添加新的key和value值,它会自动给装换成http的格式,=和&符号,比如https://xxx.xxx.xxx/xxx/xxxx/delivery?delivery_code=dqzzsm2a&timestamp=1479198840000&sign=f209c33feade99f93e8970fa0f68d3eb,我们都不用自己进行拼接和匹配了,个人觉得它用起来还是挺方便而且准确度也高,希望可以帮助到大家!

以上就是小编为大家带来的java利用httpclient进行post请求(https)实例全部内容了,希望大家多多支持~