栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

testng自动化框架入门笔记1

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

testng自动化框架入门笔记1

httpclient发请求

第一中实现:

1. 创建mavern项目并在pom.xml中添加依赖


    org.apache.httpcomponents
    httpclient
    4.5.2

2. get请求

import org.apache.http.HttpEntity;
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 java.io.IOException;
public class HttpGetNovars {
    public static void main(String[] args) throws IOException {
        String url = "https://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        CloseableHttpClient hc = HttpClients.createDefault();
        CloseableHttpResponse response = hc.execute(get);
        HttpEntity responseEntity = response.getEntity();
        String responseBody = EntityUtils.toString(responseEntity);
        System.out.println(responseBody);
    }
}

第二种实现

1. 导入依赖


  org.apache.httpcomponents.client5
  httpclient5
  5.0.1

2. get请求

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import java.io.IOException;

public class httpClientDemo1 {
    public static void main(String[] args) throws IOException, ParseException {
        String url = "https://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(get);
        int code = response.getCode();
        System.out.println(code);
        String responseString = EntityUtils.toString(response.getEntity());
        System.out.println(responseString);
        Header[] headers = response.getHeaders();
        for (Header header:headers){
            System.out.println(header.getName());
            System.out.println(header.getValue());
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/730300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号