一、httpclient简介
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
二、在idea中pom文件配置
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
</dependencies>
三、使用方法
使用HttpClient发送请求、接收响应,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6. 释放连接。无论执行方法是否成功,都必须释放连接
四、实例
1.创建配置文件src->main->resources->application.properties
test.url=http://localhost:8080
getCookies.uri=/getCookies
test.post.with.cookies=/post/with/cookies
2.创建接口类
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 {
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest(){
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(testUrl);
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> cookiesList = store.getCookies();
for (Cookie cookie : cookiesList) {
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);
//添加参数
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);
//处理结果,就是判断结果是否符合预期
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);
}
}
参考:https://blog.youkuaiyun.com/zhuwukai/article/details/78644484