测试开发接口自动化之Httpclient第一个请求

1.httpclient的第一个GET请求
1.1pom依赖文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.apigroupId>
    <artifactId>AutoApiTestartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.5.2version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.70version>
        dependency>
        <dependency>
            <groupId>org.testnggroupId>
            <artifactId>testngartifactId>
            <version>6.10version>
            <scope>testscope>
        dependency>
    dependencies>
project>
2.新建一个testBaidu文件

测试开发接口自动化之Httpclient第一个请求_第1张图片

2.1 代码如下
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;

public class testBaidu {

    @Test
    public void  get() throws IOException {


        String url = "https://www.baidu.com";

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet get = new HttpGet(url);

        CloseableHttpResponse res = httpClient.execute(get);

        System.out.println(EntityUtils.toString(res.getEntity()));

    }
}

3.执行结果 请求成功

测试开发接口自动化之Httpclient第一个请求_第2张图片

4.加上作者加群大家一起学习吧

测试开发接口自动化之Httpclient第一个请求_第3张图片

你可能感兴趣的:(测试开发,#,java测试开发路线)