httpClient官方网站:HttpClient官方网站
package com.course.httpclient.demo;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
public class MyHttpClient {
@Test
public void test1() throws IOException {
//存放结果
String result;
HttpGet get = new HttpGet("http://www.baidu.com");
//执行get方法
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
使用:ResourceBundle bundle;
来读取.properties配置文件。
bundle.getString(String str);
来读取配置文件中的key-value值。
application.properties配置文件: 内容来自于Mock框架 的 .json文件
Mock框架模拟服务器的请求与响应(method为get或者post方法)。
test.url=http://localhost:8899
dev.url=http://localhost:8899
getCookies.uri=/getCookies
login=/login
MyCookiesForGet类:通过读取配置文件来进行网页测试
package com.course.httpclient.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookiesForGet {
//读取配置文件
private String url;
private ResourceBundle bundle;
@BeforeTest
public void bforeTest(){
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 get = new HttpGet(this.url+uri);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
package com.course.httpclient.cookies;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultBackoffStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultClientConnection;
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.Locale;
import java.util.ResourceBundle;
public class MyCookiesForGet {
//读取配置文件
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void bforeTest(){
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 get = new HttpGet(this.url+uri);
//HttpClient client = HttpClientBuilder.create().build();
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookies信息
this.store = client.getCookieStore();
List<Cookie> cookieList = 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 {
String uri = bundle.getString("test.get.with.cookies");
String testUrl = this.url+uri;
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
//设置cookies信息
client.setCookieStore(store);
HttpResponse response = client.execute(get);
//获取响应的状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode = "+statusCode);
if(statusCode == 200){
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
}
}
Post请求的Mock文件:
"request"中:“json”是请求信息;“cookie”是cookie信息。
[
{
"description": "这是一个带cookies信息的post请求",
"request": {
"uri": "/post/with/cookies",
"method": "post",
"cookies": {
"login": "true"
},
"json": {
"name": "huhansan",
"age": "18"
}
},
"response": {
"status": 200,
"json": {
"huhansan":"success",
"status": "1"
}
}
}
]
MyCookiesForPost类:带cookie信息的Post请求
package com.course.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.JSONObject;
import org.testng.Assert;
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 MyCookiesForPost {
//读取配置文件properties
private String url;
private ResourceBundle bundle;
//存储Cookie信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest(){
this.bundle = ResourceBundle.getBundle("application", Locale.CHINA);
this.url = bundle.getString("test.url");
}
@Test
public void testGetCookies() throws IOException {
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
String result;
//【1】声明一个get方法
HttpGet get = new HttpGet(testUrl);
//【2】声明一个client对象
DefaultHttpClient client = new DefaultHttpClient();
//【3】声明一个response对象并用client执行get方法
HttpResponse response = client.execute(get);
//【4】声明一个String类型的对象用来查看respnse结果并输出
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//【5】获取cookies信息
store = client.getCookieStore();
List<Cookie> cookieList = 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;
//【1】声明一个Post方法
HttpPost post = new HttpPost(testUrl);
//【2】声明一个client对象,用来执行Post方法
DefaultHttpClient client = new DefaultHttpClient();
//【3】添加请求中的参数,即Mock框架中的json请求参数
JSONObject param = new JSONObject();
param.put("name","huhansan");
param.put("age","18");
//【4】设置请求头信息,即设置header
post.setHeader("content-type","application/json");
//【5】将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity);
//【6】声明一个对象来进行响应结果的存储
String result;
//【7】设置cookies信息
client.setCookieStore(this.store);
//【8】执行post方法
HttpResponse response = client.execute(post);
//【9】获取响应结果
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
System.out.println(response);
//【10】处理结果:判断结果是否符合预期
//【10.1】将返回的响应结果字符串转化为json对象
JSONObject resultJson = new JSONObject(result);
//【10.2】具体的判断返回结果的值
String success = (String) resultJson.get("huhansan");
Assert.assertEquals("success",success);
String status = (String) resultJson.get("status");
Assert.assertEquals("1",status);
}
}