1. 程式人生 > >介面自動化之httpclient發get請求、post請求

介面自動化之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\":\"
[email protected]
\"}"; 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);
    }