HttpClient testing framework (a)

1, the use of open-source Apache HttpClient framework, the official website: HTTP: //hc.apache.org/
Here Insert Picture Description
2, DefaultHttpClient outdated way to create a new HttpClient object
old: HttpClient client = new DefaultHttpClient () ;
new: HttpClient client = HttpClientBuilder. create () build ().;

3, to achieve HttpClient Get method
3.1, Moco returns a Cookies request information
[
{
"Description": "This is a feedback cookie information get request",
"Request": {
"URI": "/ the getCookies",
"Method ":" GET "
},
" Response ": {
" headers ": {
" the Content-the Type ":" text / HTML; chartset: UTF8 "
},
" Cookies ": {
" Login ":" to true "
},
" text ":" success obtained cookie "
}
}
]
3.1.1 Moco get analog interfaces
[
{
" Description ":" this is a get request feedback cookie information ",
" request ": {
" URI ":" / the getCookies ",
" Method ":" GET "
},
" Response ": {
" Cookies ": {
" Login ":" to true "
},
"Text": "success response"
}
},
{
"Description": "GET This is a tape Cookie request information",
"Request": {
"URI": "/ getWithCookies",
"Method": "GET",
"Cookies": {
"Login": "to true"
}
},
"Response": {
"headers": {
"the Content-the Type": "text / HTML; chartset: UTF8"
},
"Status": "200 is",
"text": "get this is a request with the cookie information"
}
}
]
3.1.2 Moco post analog interfaces

[

{
"Description": "Post no arguments request",
"Request": {
"URI": "/ the getCookie",
"Method": "GET"
},
"Response": {
"Cookies": {
"Login" : "to true"
},
"text": "Get cookie success"
}
},
{
"Description": "Post request no arguments",
"request": {
"URI": "/ POST / nO / param",
"Method": "POST"
},
"Response": {
"text": "Sucess"
}
},

{
"Description": "Post-band parameter request",
"Request": {
"URI": "/ POST / with / the Param",
"Method": "POST",
"Cookies": {
"Login": "to true "
},
" Forms ": {
" LoginName ":" POST ",
" password ":" 123456 "
}
},
" Response ": {
" Status ": 200 is,
" JSON ": {
" POST ":" Success ",
"Status": ". 1"
}
}
},
{
"Description": "Post with Cookies information request",
"request": {
"URI": "/ postWithCookie",
"Method": "POST",
"Cookies" : {
"Login": "to true"
},
"JSON": {
"username": "POST",
"password": "123456"
}
},
“response”: {

  "json": {
    "status": "200",
    "result": "sucess"
  }
}

}

]
3.2, Cookie and GET request to obtain a response result
package com.httpclient.cookies;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;

public class CookiesForGet {
private String RequestURL ;
private ResourceBundle bundle;
private CookieStore store;

@BeforeTest
public String getRequesturl(){
    //获取配置文件
    bundle = ResourceBundle.getBundle("application");
    RequestURL = bundle.getString("test_uri") + bundle.getString("test_method_getCookies");
    return RequestURL;

}

@Test
public void getCookie() throws IOException {
    //创建get请求
    HttpGet httpGet = new HttpGet(RequestURL) ;
    //创建httpclient对象
    DefaultHttpClient client = new DefaultHttpClient();
    //httpclient对象执行get请求
    HttpResponse response = client.execute(httpGet);
    //等到响应对象结果
    HttpEntity entity = response.getEntity();
    //响应结果转换为字符串
    String result = EntityUtils.toString(response.getEntity(),"utf-8");
    System.out.println(result);

    CookieStore store = client.getCookieStore();

    List<Cookie> cookies = store.getCookies();
    //循环遍历打印Cookies信息
    for(Cookie cookie:cookies){
        System.out.println("cookiename: " + cookie.getName() + ";"
        + "cookieValue: " + cookie.getValue()
        );
    }
}

@Test
public void getWithCookie() throws IOException {
    //
    HttpGet get = new HttpGet(RequestURL);
    DefaultHttpClient client = new DefaultHttpClient();
    client.setCookieStore(this.store);
    HttpResponse response = client.execute(get);

    String result =EntityUtils.toString(response.getEntity(),"utf8");
    int stateNum = response.getStatusLine().getStatusCode();

    System.out.println("result: "+result+"; 状态码 :"+ stateNum);

}

}

3.3 Obtaining and using cookie Cookie request sending post
package com.httpclient.cookies;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class CookiesForPost {
public String requestUrl;
public String uri1;
public ResourceBundle bundle;
public CookieStore store;
public String responseRes;

@BeforeTest
public void LoadProperties(){
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
    uri1 = bundle.getString("test_uri") ;

}

@Test//不带Cookie不带param信息的Post的请求
public void PostNoCookie() throws IOException {
    String uri2 = bundle.getString("test_postnoParam");
    requestUrl = uri1 +uri2;
    System.out.println(requestUrl);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(requestUrl);
    HttpResponse  response = client.execute(post);
    String  responseRes = EntityUtils.toString(response.getEntity(), "UTF-8");
    System.out.println(responseRes);
}

@Test
//获取Cookie
public void getCookie() throws IOException, JSONException {
    String uri2 = bundle.getString("test_getCookie");
    requestUrl = uri1 + uri2;
    HttpGet get = new HttpGet(requestUrl);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(get);
    store = client.getCookieStore();
    List<Cookie> list = store.getCookies();
    for(Cookie cookie:list){
        System.out.println("cookiename  "+ cookie.getName());
    }
}
@Test(dependsOnMethods = {"getCookie"})
//带Param带Cookie的Post请求
public void postWithCookie() throws JSONException, IOException {
    String uri3 = bundle.getString("test_postWithCookie");
    requestUrl = uri1 + uri3;
    HttpPost post = new HttpPost(requestUrl);

    DefaultHttpClient client = new DefaultHttpClient();
    //添加参数
    JSONObject param = new JSONObject();
    param.put("username","post");
    param.put("password","123456");
    //设置请求头信息 设置header
    post.setHeader("content-type","application/json");
    //将参数信息添加到方法中
    StringEntity entity = new StringEntity(param.toString(),"utf-8");

    post.setEntity(entity);
    //设置cookies信息
    client.setCookieStore(this.store);
    //执行post方法
    HttpResponse response = client.execute(post);
    //声明对象 存储响应结果
    String result = EntityUtils.toString(response.getEntity(),"utf-8");

    System.out.println("responseRes "+ result);

    //将返回的响应结果字符串转化成为json对象
    JSONObject resultJson = new JSONObject(result);
    String success = (String) resultJson.get("status");
    String status = (String) resultJson.get("result");

    System.out.println( success + status);
}

}

Published 17 original articles · won praise 0 · Views 173

Guess you like

Origin blog.csdn.net/qq_37637691/article/details/104337005