基于HttpClient在HTTP协议接口测试中的使用(详解)
程序员文章站
2024-04-01 18:12:10
http协议的接口测试中,使用到最多的就是get请求与post请求,其中post请求有form参数提交请求与raw请求,下面我将结合httpclient来实现一下这三种形式...
http协议的接口测试中,使用到最多的就是get请求与post请求,其中post请求有form参数提交请求与raw请求,下面我将结合httpclient来实现一下这三种形式:
一、get请求: get请求时,参数一般是写在链接上的,代码如下:
public void get(string url){ closeablehttpclient httpclient = null; httpget httpget = null; try { httpclient = httpclients.createdefault(); requestconfig requestconfig = requestconfig.custom().setsockettimeout(20000).setconnecttimeout(20000).build(); httpget = new httpget(url); httpget.setconfig(requestconfig); closeablehttpresponse response = httpclient.execute(httpget); httpentity httpentity = response.getentity(); system.out.println(entityutils.tostring(httpentity,"utf-8")); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally{ try { if(httpget!=null){ httpget.releaseconnection(); } if(httpclient!=null){ httpclient.close(); } } catch (ioexception e) { e.printstacktrace(); } } }
如果想把参数不写在链接上,单独的传进去,则可以这样:
public void get(string url, map<string, string> params){ closeablehttpclient httpclient = null; httpget httpget = null; try { httpclient = httpclients.createdefault(); requestconfig requestconfig = requestconfig.custom().setsockettimeout(20000).setconnecttimeout(20000).build(); string ps = ""; for (string pkey : params.keyset()) { if(!"".equals(ps)){ ps = ps + "&"; } ps = pkey+"="+params.get(pkey); } if(!"".equals(ps)){ url = url + "?" + ps; } httpget = new httpget(url); httpget.setconfig(requestconfig); closeablehttpresponse response = httpclient.execute(httpget); httpentity httpentity = response.getentity(); system.out.println(entityutils.tostring(httpentity,"utf-8")); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally{ try { if(httpget!=null){ httpget.releaseconnection(); } if(httpclient!=null){ httpclient.close(); } } catch (ioexception e) { e.printstacktrace(); } } }
二、post请求的表单提交方式,代码如下:
public void post(string url, map<string, string> params){ closeablehttpclient httpclient = null; httppost httppost = null; try { httpclient = httpclients.createdefault(); requestconfig requestconfig = requestconfig.custom().setsockettimeout(20000).setconnecttimeout(20000).build(); httppost = new httppost(url); httppost.setconfig(requestconfig); list<namevaluepair> ps = new arraylist<namevaluepair>(); for (string pkey : params.keyset()) { ps.add(new basicnamevaluepair(pkey, params.get(pkey))); } httppost.setentity(new urlencodedformentity(ps)); closeablehttpresponse response = httpclient.execute(httppost); httpentity httpentity = response.getentity(); system.out.println(entityutils.tostring(httpentity,"utf-8")); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally{ try { if(httppost!=null){ httppost.releaseconnection(); } if(httpclient!=null){ httpclient.close(); } } catch (ioexception e) { e.printstacktrace(); } } }
三、 post请求的raw参数传递:
public void post(string url, string body){ closeablehttpclient httpclient = null; httppost httppost = null; try { httpclient = httpclients.createdefault(); requestconfig requestconfig = requestconfig.custom().setsockettimeout(20000).setconnecttimeout(20000).build(); httppost = new httppost(url); httppost.setconfig(requestconfig); httppost.setentity(new stringentity(body)); closeablehttpresponse response = httpclient.execute(httppost); httpentity httpentity = response.getentity(); system.out.println(entityutils.tostring(httpentity,"utf-8")); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally{ try { if(httppost!=null){ httppost.releaseconnection(); } if(httpclient!=null){ httpclient.close(); } } catch (ioexception e) { e.printstacktrace(); } } }
以上这篇基于httpclient在http协议接口测试中的使用(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。