【java自动化1】httpClient实现POST、GET请求,application/json格式、paramters格式入参

首先IDEA中创建一个module,pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>coke</artifactId>
        <groupId>com.super.xxx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.9</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

 

在resources目录下创建一个配置文件:application.properties,里面用来存储请求域名、路径

xxx.testA.url=https://xxx.yyy.com
xxx.login.uri=/h5/login/login?
xxx.banner.uri=/api/banner/
xxx.goods.uri=/wxapi/goods/detail?

 

创建测试类

package com.course.xxx;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class XxxAuto {

    private String url;
    private ResourceBundle bundle;
//    private CookieStore store;

    @BeforeTest
    public void beforeTest(){
        bundle = ResourceBundle.getBundle("application", Locale.CHINA);
        url = bundle.getString("xxx.testA.url");
    }


    /*
    POST请求,paramenters格式入参
     */
    @Test
    public void testLogin() throws IOException {

        String uri = bundle.getString("xxx.login.uri");
        String testUrl = this.url + uri;

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(testUrl);

        // query string paramenters接口传参方式
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair("accessToken", "00001"));
        parameters.add(new BasicNameValuePair("phone", "18500009999"));
        parameters.add(new BasicNameValuePair("smsCode", "0500"));
        parameters.add(new BasicNameValuePair("scene", "C001"));
        parameters.add(new BasicNameValuePair("sign", "a2c00e8f4a17f274a61336899999"));
        // 构造一个form表单式的实体
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
        // 将请求实体设置到httpPost对象中
        post.setEntity(formEntity);

        String result;

//        client.setCookieStore(this.store);
        HttpResponse response = client.execute(post);
        result = EntityUtils.toString(response.getEntity(),"utf-8");

        // 结果转化成json对象
        JSONObject resultJson = new JSONObject(result);
        System.out.println(resultJson);

        // 获取key对应的value值
        String code = resultJson.getString("code");
//        System.out.println("code信息是:" + code);
        Assert.assertEquals("0",code);

        // 解析多层嵌套json
        String nickName = resultJson.getJSONObject("data").getString("nickname");
//        System.out.println("nickName --> "+ nickName);
        Assert.assertEquals("愚人李愚",nickName);


    }


    /*
    POST请求,json格式入参
     */
    @Test
    public void testBanner() throws IOException {
        String uri = bundle.getString("xxx.banner.uri");
        String testUrl = this.url + uri;

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(testUrl);

        JSONObject param = new JSONObject();
        param.put("accessToken","xxxx");
        param.put("cityId",120100);
        param.put("position",0);
        param.put("type","h5");

        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);

        post.setHeader("content-type","application/json");

        HttpResponse response = client.execute(post);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

        JSONObject resultJson = new JSONObject(result);
        Boolean success = resultJson.getBoolean("success");
        Assert.assertTrue(success);


    }

    /*
    GET请求,带参数
     */
    @Test
    public void testGoodsInfo() throws URISyntaxException, IOException {
        String uri = bundle.getString("xxx.goods.uri");
        String testUrl = this.url + uri;

        URIBuilder uriBuilder=new URIBuilder(testUrl);
        uriBuilder.setParameter("accessToken","xxxxx");
        uriBuilder.setParameter("latitude","39.9219");
        uriBuilder.setParameter("longitude","116.44355");
        uriBuilder.setParameter("goodsId","999");
        uriBuilder.setParameter("shopId","10000");

        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(uriBuilder.build());

        HttpResponse response = client.execute(get);
        String result = EntityUtils.toString(response.getEntity(),"utf-8");
        JSONObject resultJson = new JSONObject(result);
        System.out.println(resultJson);

        String code = resultJson.getString("code");
        Assert.assertEquals("0",code);


    }
}

 

posted @ 2021-01-04 19:31  愚人李愚  阅读(1919)  评论(0编辑  收藏  举报