httpclient与java接口_基于HttpClient在HTTP协议接口测试中的使用(详解)

本文详细介绍了如何使用HttpClient进行HTTP协议接口测试,包括GET请求的两种方式(链接上附带参数和单独传入参数)以及POST请求的表单提交和RAW参数传递。提供了具体的Java代码示例。
摘要由CSDN通过智能技术生成

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 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 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 ps = new arraylist();

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协议接口测试中的使用(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值