HttpClient发送http请求(get、post)

httpclient可以模拟发送http请求
接口:接口分为对内接口、对外接口
对内:接口与接口之间的调用
对外:就是像springboot一样,前端请求后能返回数据到页面
什么是接口测试:
1 测试系统组件间接口的一种测试
2 检测外部系统与系统之间以及内部各个子系统之间的交互点
3 测试的重点是要检测数据的交换、传递和业务逻辑处理的过程
测试的工具
webdriver:自动化测试工具 底层是靠接口传递的,并且是restful风格的接口
appium:自动化测试工具 底层也是靠接口传递的
如何通过httpclient调用
通信原理:域名解析,若请求的本地有服务器、远程服务器也有相同的服务名称,那么浏览器访问到的是本地
例 C:\Windows\System32\drivers\etc\hosts 在该文档中编辑 127.0.0.1 www.baidu.com 保存成功之后,在浏览器访问就只能访问到本地的地址

要使用httpclient第一步引包

       <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>

有一个对外接口,请求方法为get

@Controller
@RequestMapping("/test")
public class SpringBootTest {
@RequestMapping(value = "/test1", params = {"name","age"},method = RequestMethod.GET)
public @ResponseBody
String getString(@RequestParam String name,@RequestParam Integer age) {
    return "姓名"+name+"年龄"+age;
}
}

用httpclient如何请求上面的这个对外接口,get请求方法
方法一:请求参数与请求地址在一起

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
public class InterfaceForHttp {
@Test
public void getTest(){
    //定义请求地址,参数与请求地址在一起
    String url="http://127.0.0.1:8080/test/test1?name=xx&age=10";
    //创建一个默认的客户端
    HttpClient httpClient=HttpClients.createDefault();
    //创建一个get请求,get请求方法传地址参数
    HttpGet httpGet=new HttpGet(url);
    try {
        /**
         *执行httpget请求方法,请求完成之后会有相应结果
         * 相应的是HttpResponse,用HttpResponse接收相应结果
        */
        HttpResponse httpResponse=httpClient.execute(httpGet);
        //从相应数据中获取到返回值,用getEntity方法,获取到的是一个对象
        HttpEntity httpEntity=httpResponse.getEntity();
        //将该获取的对象转为字符串
        String result=EntityUtils.toString(httpEntity);
        System.out.println(result);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

在这里插入图片描述
方法二:将请求参数与请求地址分离,这样要增加参数、修改参数就不会直接在url上改来改去

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException;
public class InterfaceForHttp {
@Test
public void getTest(){
    //定义请求地址,只写请求的IP+端口+路径
    String url="http://127.0.0.1:8080/test/test1";
    //创建一个默认的客户端
    HttpClient httpClient=HttpClients.createDefault();
    try {
        //添加请求路径到uriBuilder
        URIBuilder uriBuilder=new URIBuilder(url);
        //添加请求参数,添加多个参数用setParameter,添加一个参数用addParameter
        uriBuilder.setParameter("name","xx" );
        uriBuilder.setParameter("age","10" );
        //创建get请求
        HttpGet httpGet=new HttpGet(uriBuilder.build());
        //发送get请求,将相应数据用HttpResponse接收
        HttpResponse httpResponse=httpClient.execute(httpGet);
        //从相应数据中获取返回值,获取到的是一个对象
        HttpEntity httpEntity=httpResponse.getEntity();
        //从相应数据中获取code码
        int code=httpResponse.getStatusLine().getStatusCode();
        //将获取的返回对象转为字符串
        String result=EntityUtils.toString(httpEntity);
        System.out.println("请求code码:"+code+";请求返回:"+result );
        } catch (URISyntaxException e) {
        e.printStackTrace();
        }catch (IOException e) {
        e.printStackTrace();
        }
    }
}

在这里插入图片描述
有一个对外接口,请求方法为post

实体类

import lombok.Data;
@Data
public class Animal {
private String name;
private Integer age;
private char sex;
}

对外接口

import com.longteng.lesson2.my.animal.Animal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/test")
public class SpringBootTest {
@RequestMapping(value = "/test4",method = RequestMethod.POST)
public @ResponseBody
Animal test4( @RequestBody Animal animal) {
    return animal;
}
}

httpclient发送post请求

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class InterfaceForHttp {
@Test
public void postTest() {
    //请求路径:IP+端口+路径
    String url="http://127.0.0.1:8080/test/test4";
    //创建一个默认客户端
    HttpClient httpClient=HttpClients.createDefault();
    //post请求,将url以参数传递
    HttpPost httpPost=new HttpPost(url);
    //请求参数,post请求只能用json请求,性别是char类型,用单引号
    String parameter="{\"name\":\"xx\", \"age\":10,\"sex\":\'男\'}";
    try {
        //添加头信息,post请求要加上Content-Type
        httpPost.addHeader("Content-Type", "application/json");
        //传入请求参数,传入请求参数时就设置编码为utf-8
        StringEntity stringEntity=new StringEntity(parameter,"UTF-8");
        //post请求set参数
        httpPost.setEntity(stringEntity);
        //发送post请求,响应数据用HttpResponse接收
        HttpResponse httpResponse=httpClient.execute(httpPost);
        //从HttpResponse相应数据中获取code码
        int code=httpResponse.getStatusLine().getStatusCode();
        //将响应数据(对象)转为string类型,设置编码为utf-8
        String result= EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
        System.out.println("请求code码:"+code+";请求返回:"+result );
        }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        }catch (IOException e) {
        e.printStackTrace();
        }
    }
}

post请求要加Content-Type,参数要以json传递,这里有个坑,请求时要设置参数编码,获取的返回也要设置,否则遇到汉字就只显示问号,并且要两个一起设置
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41767337/article/details/89213873