httpclient幾種請求方式


一、httpclient 模擬get請求,並獲取cookie信息

 

public class MyCookiesForGet {

    private String url;
    //用來讀取.properties的配置文件
    private ResourceBundle bundle;
    private CookieStore store;
    @BeforeTest
    public void beforeTest() {
        //讀取加載.properties的配置文件
        bundle = ResourceBundle.getBundle("application",Locale.CHINA);
        url = bundle.getString("test.url");
    }
    @Test
    public void testGetCookies() throws IOException {
        String result;
        //從配置文件中 拼接測試的url
        String uri = bundle.getString("getCookies.uri");
        String testUrl = this.url + uri;

        //HttpGet 相當於在瀏覽器地址欄輸入一個地址
        HttpGet get = new HttpGet(testUrl);
        //DefaultHttpClient 相當於一個瀏覽器,先new一個瀏覽器
        DefaultHttpClient client = new DefaultHttpClient();

        //client.execute(get) 相當於用瀏覽器打開網頁了,獲得一個response結果
        HttpResponse response = client.execute(get);

        //response.getEntity() 打印請求的實體內容 返回json格式
        //EntityUtils.toString() 將json格式實體內容轉換成字符串String
        result = EntityUtils.toString(response.getEntity(),"utf-8");

        System.out.println(result);

        //獲取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = this.store.getCookies();

        for (Cookie cookie : cookieList) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie name = "+name+",cookie value = "+value);
        }
  
    }
}

 

 

二、httpclient 模擬攜帶cookie信息進行的get請求

 

public class MyCookiesForGet {

    private String url;
    //用來讀取.properties的配置文件
    private ResourceBundle bundle;
    private CookieStore store;
    @BeforeTest
    public void beforeTest() {
        //讀取加載.properties的配置文件
        bundle = ResourceBundle.getBundle("application",Locale.CHINA);
        url = bundle.getString("test.url");
    }
    @Test
    public void testGetCookies() throws IOException {
        String result;
        //從配置文件中 拼接測試的url
        String uri = bundle.getString("getCookies.uri");
        String testUrl = this.url + uri;

        //HttpGet 相當於在瀏覽器地址欄輸入一個地址
        HttpGet get = new HttpGet(testUrl);
        //DefaultHttpClient 相當於一個瀏覽器,先new一個瀏覽器
        DefaultHttpClient client = new DefaultHttpClient();

        //client.execute(get) 相當於用瀏覽器打開網頁了,獲得一個response結果
        HttpResponse response = client.execute(get);

        //response.getEntity() 打印請求的實體內容 返回json格式
        //EntityUtils.toString() 將json格式實體內容轉換成字符串String
        result = EntityUtils.toString(response.getEntity(),"utf-8");

        System.out.println(result);

        //獲取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = this.store.getCookies();

        for (Cookie cookie : cookieList) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie name = "+name+",cookie value = "+value);
        }


    }
    @Test(dependsOnMethods = {"testGetCookies"})
    public void testGetWithCookies() throws IOException {
        //測試url拼接,使用ResourceBundle bundle 來讀取.properties文件配置內容
        String uri = bundle.getString("test.get.with.cookies");
        String testUrl = this.url + uri;
        
        //HttpGet 相當於在瀏覽器中輸入url
        HttpGet get = new HttpGet(testUrl);
        
        //DefaultHttpClient 相當於創建打開一個瀏覽器
        DefaultHttpClient client = new DefaultHttpClient();

        //設置cookies信息
        client.setCookieStore(this.store);
        
        //HttpResponse 相當於瀏覽器打開網頁,並或得了響應結果
        HttpResponse response = client.execute(get);

        //獲取響應狀態碼
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("statusCode = "+statusCode);

        if(statusCode == 200) {
            //獲取響應格式為json的實體內容,並轉換為字符串
            String result = EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(result);
        }
    }
}

 

 

三、httpclient模擬攜帶cookie信息的post請求(請求格式為json)

 

public class MyCookiesForPost {
    private String url;
    //用來讀取.properties的配置文件
    private ResourceBundle bundle;
    private CookieStore store;
    @BeforeTest
    public void beforeTest() {
        //讀取加載.properties的配置文件
        bundle = ResourceBundle.getBundle("application",Locale.CHINA);
        url = bundle.getString("test.url");
    }
    @Test
    public void testGetCookies() throws IOException {
        String result;
        //從配置文件中 拼接測試的url
        String uri = bundle.getString("getCookies.uri");
        String testUrl = this.url + uri;

        //HttpGet 相當於在瀏覽器地址欄輸入一個地址
        HttpGet get = new HttpGet(testUrl);
        //DefaultHttpClient 相當於一個瀏覽器,先new一個瀏覽器
        DefaultHttpClient client = new DefaultHttpClient();

        //client.execute(get) 相當於用瀏覽器打開網頁了,獲得一個response結果
        HttpResponse response = client.execute(get);

        //response.getEntity() 打印請求的實體內容 返回json格式
        //EntityUtils.toString() 將json格式實體內容轉換成字符串String
        result = EntityUtils.toString(response.getEntity(),"utf-8");

        System.out.println(result);

        //獲取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = this.store.getCookies();

        for (Cookie cookie : cookieList) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie name = "+name+",cookie value = "+value);
        }


    }
    @Test(dependsOnMethods = {"testGetCookies"})
    public void testPostMethod() throws IOException {
        String uri = bundle.getString("test.post.with.cookies");
        //拼接最終的測試地址
        String testUrl = this.url + uri;

        //聲明一個client對象,用來進行方法的執行;相當於打開一個瀏覽器
        DefaultHttpClient client = new DefaultHttpClient();
        //聲明一個方法,這個方法就是post方法;相當於在瀏覽器中輸入一個請求地址
        HttpPost post = new HttpPost(testUrl);
        //添加參數(json格式類型的請求參數)
        JSONObject param = new JSONObject();
        param.put("name","huhansan");
        param.put("age","18");
        //設置請求頭信息 設置header
        post.setHeader("content-type","application/json");
        //將參數信息添加到方法中,將參數綁定到請求信息中
        StringEntity entity = new StringEntity(param.toString(),"utf-8");
        post.setEntity(entity);
        //聲明一個對象用來進行響應結果的存儲
        String result;
        //設置cookies信息
        client.setCookieStore(this.store);
        //執行post方法
        HttpResponse response = client.execute(post);
        //獲取響應結果
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
        //處理結果,判斷返回結果是否符合預期
        //將返回的響應結果字符串轉換成json對象
        JSONObject resultJson = new JSONObject(result);
        //判斷具體的返回結果值
        String success = (String) resultJson.get("huhansan");
        String status = (String) resultJson.get("status");
        Assert.assertEquals("success",success);
        Assert.assertEquals("1",status);


    }
}

 

 

四、httpclient模擬攜帶cookie信息的post請求(請求格式為表單)

 

public class MyCookiesForPost {
    private String url;
    private ResourceBundle bundle;
    //用來存儲cookies信息的變量
    private CookieStore store;
    @BeforeTest
    public void beforeTest() {
        bundle = ResourceBundle.getBundle("application", Locale.CHINA);
        url = bundle.getString("wms.login.url");
    }

    @Test
    public void testGetCookies() throws IOException {
        String result;
        String uri = bundle.getString("wms.login.uri");
        String testUrl = this.url + uri;

        //測試邏輯代碼編寫
        HttpGet get = new HttpGet(testUrl);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        System.out.println(response);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        //System.out.println(result);

        //獲取cookies信息
        this.store = client.getCookieStore();
        List<Cookie> cookieList = this.store.getCookies();
        for(Cookie cookie : cookieList) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie name = " + name + ";cookie value = "+value);
        }
    }
    @Test(dependsOnMethods = {"testGetCookies"})
    public void testPostMethod() throws IOException {
        String uri = bundle.getString("wms.login.validate");
        //拼接最終測試地址
        String testUrl = this.url + uri;

        //聲明一個client對象,用來進行方法的執行
        DefaultHttpClient client = new DefaultHttpClient();
        //聲明一個方法,這個方法就是post方法
        HttpPost post = new HttpPost(testUrl);
        //添加參數
        List <NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("username","ldcx"));
        nvps.add(new BasicNameValuePair("password","123456"));
        //設置請求頭信息 設置header
        post.setHeader("content-type","application/x-www-form-urlencoded");
        //將參數信息添加到方法中
        HttpEntity entity = new UrlEncodedFormEntity(nvps,"utf-8");
        //StringEntity entity = new StringEntity(nvps.toString(),"utf-8");
        post.setEntity(entity);
        System.out.println("請求url地址"+post.getURI());
        //聲明一個對象來進行響應結果的存儲
        String result;
        //設置cookies信息
        client.setCookieStore(this.store);
        //執行post請求
        HttpResponse response = client.execute(post);
        //獲取響應結果
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

    }
    @Test(dependsOnMethods = {"testPostMethod"})
    public void testGetWareHouse() throws IOException {
        String result;
        String uri = bundle.getString("wms.warehouse");
        String testUrl = this.url + uri;

        //測試邏輯代碼編寫
        HttpGet get = new HttpGet(testUrl);
        DefaultHttpClient client = new DefaultHttpClient();
        client.setCookieStore(this.store);
        HttpResponse response = client.execute(get);
        System.out.println(response);
        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM