使用httpclient构造http/https客户端

目前接口测试使用jmeter工具配置服务器IP、端口号、协议、url路径、请求方法、请求参数,参考jmeter的设计我们可以用编程语言自己实现一个客户端,本文主要讲一下使用httpclient构造http/https客户端的代码实现。
1.公共请求参数类,主要包括IP、端口号、协议、所有请求组成的链表

import java.util.List;
/**
 * ClassName: RequestParameter
 * 
 * @author xiezhaoqing192
 * @date 2016年4月26日
 */
public class RequestParameter {
 private String host;
 private int port;
 private String protocol;
 private List<BasicParameter> basparams;
 
 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }
 
 public String getProtocol() {
  return protocol;
 }

 public void setProtocol(String protocol) {
  this.protocol = protocol;
 }
 
 public List<BasicParameter> getBasparams() {
  return basparams;
 }

 public void setBasparams(List<BasicParameter> basparams) {
  this.basparams = basparams;
 }

}

2.具体的请求参数类

import java.util.HashMap;
import java.util.Map;
/**
 * ClassName: BasicParameter
 * 
 * @author xiezhaoqing192
 * @date 2016年4月26日
 */
public class BasicParameter {
    private String apiUrl;
    private String method;
    private Map<String,String> paramters=new HashMap<String,String>();
    private String uniqueid;
    private Map<String,String> fileList=new HashMap<String,String>(); 
    //新增逻辑,支持后置处理器 2016.05.20 by djj
    private Map<String,String> afterCatch=new HashMap<String,String>(); 
    /**
     * 默认构造函数
     * @param String apiUrl,String method,String uniqueid
     * @return N/A
     * @throws N/A
     */
    public BasicParameter(String apiUrl,String method,String uniqueid){
        this.apiUrl=apiUrl;
        this.method=method;
        this.uniqueid=uniqueid;
        
    }
    public String getApiUrl() {
        return apiUrl;
    }

    public void setApiUrl(String apiUrl) {
        this.apiUrl = apiUrl;
    }
    
    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }
    
    public Map<String,String> getParamters() {
        return paramters;
    }

    public void addParamters(String key,String value) {
        paramters.put(key, value);
    }
    public void addFile(String key,String value) {
        fileList.put(key, value);
    }
    public void addAfterCatch(String key,String value) {
        afterCatch.put(key, value);
    }
    
    public String getUniqueid() {
        return uniqueid;
    }

    public void setUniqueid(String uniqueid) {
        this.uniqueid = uniqueid;
    }
    public Map<String,String> getAfterCatch() {
        return afterCatch;
    }
    /**
     * @return the fileList
     */
    public Map<String,String> getFileList() {
        return fileList;
    }
    /**
     * @param fileList the fileList to set
     */
    public void setFileList(Map<String,String> fileList) {
        this.fileList = fileList;
    }
}

3.响应参数类

import java.util.HashMap;
import java.util.Map;
import org.apache.http.Header;
/**
 * ClassName: BasicResponse
 * 
 * @author xiezhaoqing192
 * @date 2016年4月26日
 */
public class ResponseParameter {
    private int respcode;
    private String respdata;
    private String respmsg;
    private String uniqueid;
    private int runtime;
    private String urlString;
    private Map<String,String>headers=new HashMap<String,String>();
    
    public Integer getRespcode() {
        return respcode;
    }
    public void setRespcode(Integer respcode) {
        this.respcode = respcode;
    }
    public String getRespmsg() {
        return respmsg;
    }
    public void setRespmsg(String respmsg) {
        this.respmsg = respmsg;
    }
    public String getUniqueid() {
        return uniqueid;
    }

    public void setUniqueid(String uniqueid) {
        this.uniqueid = uniqueid;
    }
    
    public Integer getRuntime() {
        return runtime;
    }

    public void setRuntime(Integer runtime) {
        this.runtime = runtime;
    }

    /**
     * @return the headers
     */
    public Map<String,String> getHeaders() {
        return headers;
    }

    /**
     * @param headers the headers to set
     */
    public void setHeaders(Header[] headers) {
        if(headers!=null)
        for(Header header:headers)
        {
            this.headers.put(header.getName(), header.getValue());
        }
    }

    /**
     * @return the urlString
     */
    public String getUrlString() {
        return urlString;
    }

    /**
     * @param urlString the urlString to set
     */
    public void setUrlString(String urlString) {
        this.urlString = urlString;
    }

    /**
     * @return the respdata
     */
    public String getRespdata() {
        return respdata;
    }

    /**
     * @param respdata the respdata to set
     */
    public void setRespdata(String respdata) {
        this.respdata = respdata;
    }
}

4.创建URI

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
/**
 * ClassName: HttpURIBuilder
 * 
 * @author xiezhaoqing192
 * @date 2016年4月26日
 */
public class HttpURIBuilder {

  public static URI buildHttpURI(String host, int port, String api,
    List<NameValuePair> nvps,String protocol) {

   URI uri = URI.create("");
   try {
    URIBuilder builder = new URIBuilder();
    builder.setScheme(protocol);
    builder.setHost(host);
    builder.setPort(port);
    builder.setPath(api); 
    if(null != nvps)
     for (NameValuePair nvp:nvps){
      builder.setParameter(nvp.getName(), nvp.getValue());
     }
    uri = builder.build();
   } catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } 
   return uri;
  }
}

5.HttpClient具体实现类

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.entity.mime.content.FileBody; 
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
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.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.entity.mime.MultipartEntity;  
import  org.apache.http.entity.mime.content.StringBody;
/**
 * ClassName: HttpResImpl
 * @warming 这个类为核心类,封装了基本请求,后置处理器,类正则提取,请勿更改任何逻辑 
2016.05.20 at丁晶晶
 * @author xiezhaoqing192
 * @date 2016年4月26日
 */
@SuppressWarnings("deprecation")
public class HttpResImpl implements HttpRes{
    private  Logger logger = LoggerFactory.getLogger(HttpResImpl.class);
    private  HttpClient httpclient = null;
    private  HttpResponse response = null;
    private Map<String,String> afterCatchPool=new HashMap<String,String>(); 
    
    @Override
    public HttpResponse getInstance() {
        if (null == response) {
            response = new BasicHttpResponse(new ProtocolVersion("HTTP_ERROR", 1, 1), 500, "ERROR");
        }
        return response;
    }

    @Override
    public HttpClient createHttpClient(String protocol) {
        HttpParams params = new BasicHttpParams();
        // 连接超时
        HttpConnectionParams.setConnectionTimeout(params, HttpConstants.TIMEOUT);
        // 请求超时
        HttpConnectionParams.setSoTimeout(params, HttpConstants.TIMEOUT);
        //保持长连接设置,后期将这个引出吧
    //  HttpConnectionParams.setSoKeepalive(params, false);
        
        // 设置我们的HttpClient支持HTTP和HTTPS两种模式
        SchemeRegistry schReg = new SchemeRegistry();
        if ("HTTP".equals(protocol.toUpperCase())) {
            schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        } else if ("HTTPS".equals(protocol.toUpperCase())) {
            // 创建TrustManager()
            // 用于解决javax.net.ssl.SSLPeerUnverifiedException: peer not
            // authenticated
            X509TrustManager trustManager = 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;
                }
            };

            // 创建HostnameVerifier
            // 用于解决javax.net.ssl.SSLException: hostname in certificate didn't
            // match: <202.69.27.140> != <*.pingan.com.cn>
            X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
                @Override
                public void verify(String host, SSLSocket ssl) throws IOException {
                }

                @Override
                public void verify(String host, X509Certificate cert) throws SSLException {
                }

                @Override
                public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
                }

                @Override
                public boolean verify(String arg0, SSLSession arg1) {
                    return true;
                }
            };
            SSLContext sslContext = null;
            try {
                sslContext = SSLContext.getInstance(SSLSocketFactory.TLS);
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
            try {
                sslContext.init(null, new TrustManager[] { trustManager }, null);
            } catch (KeyManagementException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 创建SSLSocketFactory
            SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, hostnameVerifier);
            schReg.register(new Scheme("https", socketFactory, 443));
        } else {
            logger.error("不支持除http/https之外的协议");
        }

        // 使用线程安全的连接管理来创建HttpClient
        ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
        if(httpclient==null){
        httpclient = new DefaultHttpClient(conMgr, params);
        }

        return httpclient;

    }

    @Override
    public List<ResponseParameter> execute(RequestParameter reqparams) {
        httpclient = createHttpClient(reqparams.getProtocol());
        response = getInstance();
        List<ResponseParameter> resparams = new ArrayList<ResponseParameter>();

        for (BasicParameter bp : reqparams.getBasparams()) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params = MapToNvps.convertToNvps(bp.getParamters(),afterCatchPool);
        
            ResponseParameter rp = new ResponseParameter();
            long startMili = 0;//定义开始时间
            long endMili=0;//定义结束时间
            URI request = HttpURIBuilder.buildHttpURI(reqparams.getHost(), reqparams.getPort(), bp.getApiUrl(), params,
                    reqparams.getProtocol());
            String urlString=request.toString();
            try {

                if ("GET".equals(bp.getMethod().toUpperCase())) {
                    HttpGet hg = new HttpGet(request);
                    startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
                    response = httpclient.execute(hg);
                    endMili=System.currentTimeMillis();// 当前时间对应的毫秒数-结束
                }

                else if ("POST".equals(bp.getMethod().toUpperCase())) {
                    HttpPost httppost = new HttpPost(request);
                     MultipartEntity entity = new MultipartEntity();
                     if(bp.getFileList()!=null)
                    for( Map.Entry<String, String> filePara:bp.getFileList().entrySet()){
                        try{
                           File file = new File(filePara.getValue());
                           FileBody fileBody = new FileBody(file);  
                            entity.addPart(filePara.getKey(), fileBody); 
                           
                        }
                        catch(Exception e){
                            
                        }
                        
                    };
                    for(Map.Entry<String, String> para:bp.getParamters().entrySet()){
                        StringBody paraMeters = new StringBody(para.getValue()); 
                        entity.addPart(para.getKey(), paraMeters);
                    }
                     httppost.setEntity(entity);  
                    startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
                    response = httpclient.execute(httppost);
                    endMili=System.currentTimeMillis();// 当前时间对应的毫秒数-结束

                }

                else if ("PUT".equals(bp.getMethod().toUpperCase())) {
                    HttpPut p = new HttpPut(request);
                    startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
                    response = httpclient.execute(p);
                    endMili=System.currentTimeMillis();// 当前时间对应的毫秒数-结束
                }

                else if ("DELETE".equals(bp.getMethod().toUpperCase())) {
                    HttpDelete hd = new HttpDelete(request);
                    startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
                    response = httpclient.execute(hd);
                    endMili=System.currentTimeMillis();// 当前时间对应的毫秒数-结束
                }
                rp.setHeaders(response.getAllHeaders());
                rp.setRespcode(response.getStatusLine().getStatusCode());
                rp.setRespmsg(response.getStatusLine().getReasonPhrase());
                rp.setUniqueid(bp.getUniqueid());
                rp.setUrlString(urlString);
                rp.setRuntime((int) (endMili-startMili));
                try{
                rp.setRespdata(EntityUtils.toString(response.getEntity(),"UTF-8"));}
                catch(IOException e){
                    rp.setRespdata("自动化平台不支持的返回对象");
                }
                
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                endMili=System.currentTimeMillis();
                e.printStackTrace();  
                rp.setHeaders(null);
                rp.setRespcode(408);
                rp.setRespdata(null);
                rp.setRespmsg("找不到服务器:"+e.getMessage());
                rp.setUniqueid(bp.getUniqueid());
                rp.setRuntime((int) (endMili-startMili));
                rp.setUrlString(urlString);
            }
             catch (ConnectTimeoutException e) {
                    // TODO Auto-generated catch block
                    endMili=System.currentTimeMillis();
                    e.printStackTrace();  
                    rp.setHeaders(null);
                    rp.setRespcode(408);
                    rp.setRespdata(null);
                    rp.setRespmsg("连接超时:"+e.getMessage());
                    rp.setUniqueid(bp.getUniqueid());
                    rp.setRuntime((int) (endMili-startMili));
                    rp.setUrlString(urlString);
                }
            
            catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                endMili=System.currentTimeMillis();
                e.printStackTrace();  
                rp.setHeaders(null);
                rp.setRespcode(408);
                rp.setRespdata(null);
                rp.setRespmsg(e.getMessage());
                rp.setUniqueid(bp.getUniqueid());
                rp.setRuntime((int) (endMili-startMili));
                rp.setUrlString(urlString);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                endMili=System.currentTimeMillis();
                e.printStackTrace();
                rp.setHeaders(null);
                rp.setRespcode(408);
                rp.setRespdata(null);
                rp.setRespmsg(e.getMessage());
                rp.setUniqueid(bp.getUniqueid());
                rp.setRuntime((int) (endMili-startMili));
                rp.setUrlString(urlString);
            } 
            catch (Exception e) {
                endMili=System.currentTimeMillis();
                e.printStackTrace();
                rp.setHeaders(null);
                rp.setRespcode(408);
                rp.setRespdata(null);
                rp.setRespmsg(e.getMessage());
                rp.setUniqueid(bp.getUniqueid());
                rp.setRuntime((int) (endMili-startMili));
                rp.setUrlString(urlString);
                
            }
        
            
            
            resparams.add(rp);
            String responseString;
        
            //获取接口的json
            Map<String,String>afterCatchMap= bp.getAfterCatch();//获取后置处理器
            for(Map.Entry<String,String> afterCatch:afterCatchMap.entrySet()){
            
            
                //这里异常不抛出去,如果这里出现错误,则直接给后置处理器赋空
            try {
                    responseString = rp.getRespdata();//获取接口的json
                     String afterCatchValue=JsonHelper.getValueInJsonStringByKey(afterCatch.getValue(), responseString);//获取json提取值
                     afterCatchPool.put(afterCatch.getKey(), afterCatchValue);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                     afterCatchPool.put(afterCatch.getKey(), null);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                     afterCatchPool.put(afterCatch.getKey(), null);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                     afterCatchPool.put(afterCatch.getKey(), null);
                }
                
            }
            
        }

        return resparams;

    }

    /**
     * @return the afterCatchPool
     */
    public Map<String,String> getAfterCatchPool() {
        return afterCatchPool;
    }

    /**
     * @param afterCatchPool the afterCatchPool to set
     */
    public void setAfterCatchPool(Map<String,String> afterCatchPool) {
        this.afterCatchPool = afterCatchPool;
    }

}

以上就实现了支持http/https协议的client。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,580评论 18 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 10,849评论 6 13
  • 名词延伸 通俗的说,域名就相当于一个家庭的门牌号码,别人通过这个号码可以很容易的找到你。如果把IP地址比作一间房子...
    杨大虾阅读 20,585评论 2 57
  • 这部电影看完后,始终没发现题目跟内容有什么大的关系,除了里面有男主角为女主角画肖像这一情节能搭上边外,其他的感觉没...
    字爵阅读 4,136评论 0 0
  • 看到电视剧中恶俗的情节会变态一般的重现;周末会嫌我是电灯泡,两人单独出去约会;总是吵吵闹闹的要生二胎,然后当着我这...
    怒吃三大碗阅读 299评论 0 0