接口自动化之httpclient发get请求、post请求

package com.second;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.junit.Test;

import java.io.IOException;

/**
 * Created by Administrator on 2017/8/10.
 */
public class HttpClientTest {
    @Test
    public void getUserByIdTest() throws IOException {
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/DemoController/getUserById?id=1";
        GetMethod getMethod = new GetMethod(url);
        int status = httpClient.executeMethod(getMethod);
        String body = getMethod.getResponseBodyAsString();
        System.out.print("返回的状态码:"+status+"返回的结果是:"+body);
    }
    @Test
    public void getUserById() throws IOException {
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/DemoController/getUserById";
        GetMethod getMethod = new GetMethod(url);
        //两个参数的时候2  new两个对象
        NameValuePair[] nameValuePairs = new NameValuePair[1];

        NameValuePair nameValuePair = new NameValuePair();
        nameValuePair.setName("id");
        nameValuePair.setValue("1");
        nameValuePairs[0] = nameValuePair;

//        NameValuePair nameValuePair2 = new NameValuePair();
//        nameValuePair2.setName("name");
//        nameValuePair2.setValue("longteng");
//        nameValuePairs[1] = nameValuePair2;
        //设置编码格式
        HttpClientParams clientParams = httpClient.getParams();
        clientParams.setContentCharset("UTF-8");

        //设置超时
        HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
        params.setConnectionTimeout(5000);
        params.setSoTimeout(1000*60);

        getMethod.setQueryString(nameValuePairs);

        int status = httpClient.executeMethod(getMethod);
        String body = getMethod.getResponseBodyAsString();

        System.out.print("返回状态码:"+status+";返回结果"+body);

    }
}



  //post接口
    @Test
    public void postMethod () throws IOException {
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/DemoController/getUserByIdPost?id=1";
        PostMethod postMethod = new PostMethod(url);
        int status = httpClient.executeMethod(postMethod);
        String body = postMethod.getResponseBodyAsString();
        System.out.print("返回状态码是:"+status+";返回结果是:"+body);
    }


    //带body体的 rest风格接口  用setRequestBody
    @Test
    public void postMethod1 (){
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/DemoController/modifyUserByIdBody/1";
        String body = "{\"name\":\"longtest\",\"email\":\"long@1111.com\"}";
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestBody(body);
        try {
            int status = httpClient.executeMethod(postMethod);
            String body1 = postMethod.getResponseBodyAsString();
            System.out.print("返回状态码是:"+status+";返回结果是:"+body1);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //授权  授权信息加在header里 用setRequestHeader
    @Test
    public void authorizationTest(){
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/DemoController/getUserByIdAuth?id=1";
        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Authorization","bG9uZ3Rlbmc6dGVzdA==");

        try {
            int status = httpClient.executeMethod(getMethod);
            String body = getMethod.getResponseBodyAsString();
            System.out.print("返回状态码:"+status+"返回结果是"+body);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


 //带cookie的,登录请求,服务器把sessionid 放到cookie里
    @Test
    public void cookieTest() throws IOException {
        HttpClient httpClient = new HttpClient();
        String url1 = "http://localhost:8881/LoginController/login?password=a&username=a";
        PostMethod postMethod = new PostMethod(url1);
        int status1 = httpClient.executeMethod(postMethod);
        String body1 = postMethod.getResponseBodyAsString();
        System.out.print("返回状态码:"+status1+"返回结果"+body1);

        Cookie[] cookies = httpClient.getState().getCookies();
        for (Cookie c:cookies){
           System.out.print("name"+c.getName()+",,,,,value"+c.getValue());
        }

        String url = "http://localhost:8881/DemoController/getUserByIdSession?id=1";
        GetMethod getMethod = new GetMethod(url);
        try {
            int status = httpClient.executeMethod(getMethod);
            String body = getMethod.getResponseBodyAsString();
            System.out.print("返回状态码:"+status+";返回结果是"+body);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //代理请求
    @Test
    public void proxyTest() throws IOException {
        HttpClient httpClient = new HttpClient();
        String url ="http:www.baidu.com";
        httpClient.getHostConfiguration().setProxy("127.0.0.1",8881);

        GetMethod getMethod = new GetMethod(url);
        int status = httpClient.executeMethod(getMethod);
        String body = getMethod.getResponseBodyAsString();

        System.out.print("返回状态码:"+status+";返回结果是"+body);
    }

    //重定向
    @Test
    public void forwardTest() throws IOException {
        HttpClient httpClient = new HttpClient();
        String url = "http://localhost:8881/forWard.jsp?id=1";
        GetMethod getMethod = new GetMethod(url);
        //第一种方式自动跟随重定向一般是打开的true ,如果关闭返回状态码就是302
        getMethod.setFollowRedirects(false);
        int status = httpClient.executeMethod(getMethod);
        String body = getMethod.getResponseBodyAsString();

        System.out.print("返回状态码:"+status+";返回结果是"+body);
        //第二种方式 header 这里的url1 是真正的请求地址
        Header header = getMethod.getResponseHeader("location");
        String url1 = header.getValue();
        System.out.print(url1);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值