Header、Cookie
1、Header
1、创建Headers类。
添加2个Header:
内容格式设定为Json格式("content-type", "application/json")、自定义Header("Self-Header", "MySelfHeader")。
脚本代码:
package com.test.demo;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
* Header
*
* @author wangmcn
*
*/
public class Headers {
public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建CloseableHttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建HttpPost对象
HttpPost httpPost = new HttpPost("http://localhost:8083/header");
// 设置请求头信息
httpPost.setHeader("content-type", "application/json");
httpPost.setHeader("Self-Header", "MySelfHeader");
// 添加Json参数
JSONObject param = new JSONObject();
param.put("username", "admin");
param.put("password", "123456");
// 将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(), "utf-8");
httpPost.setEntity(entity);
// 执行Post请求
CloseableHttpResponse response = httpclient.execute(httpPost);
// 获取响应状态
System.out.println("获取响应状态: " + response.getStatusLine().getStatusCode());
// 获取响应结果
String result = EntityUtils.toString(response.getEntity(), "utf-8");
// 将返回的响应结果字符串转化成为Json对象
JSONObject resultJson = new JSONObject(result);
// 获取网页源码
System.out.println("获取网页源码:" + resultJson);
// 获取请求头
Header requestHeader[] = httpPost.getAllHeaders();
for (Header header : requestHeader) {
System.out.println("请求头: " + header.getName() + ": " + header.getValue());
}
// 获取响应头
Header responseHeader[] = response.getAllHeaders();
for (Header header : responseHeader) {
System.out.println("响应头: " + header.getName() + ": " + header.getValue());
}
// 关闭流和释放系统资源
response.close();
// 关闭客户端
httpclient.close();
}
}
2、运行结果:
2、Cookie
1、创建Cookies类。
创建CookieStore对象,创建BasicClientCookie对象,添加Cookie。
脚本代码:
package com.test.demo;
import java.io.IOException;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
/**
* Cookie
*
* @author wangmcn
*
*/
public class Cookies {
public static void main(String[] args) throws ClientProtocolException, IOException {
// 创建CookieStore对象
CookieStore cookieStore = new BasicCookieStore();
// 创建BasicClientCookie对象,添加Cookie
BasicClientCookie cookie = new BasicClientCookie("login", "true");
cookie.setVersion(0);
cookie.setDomain("localhost");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// 创建CloseableHttpClient对象
CloseableHttpClient httpclient = HttpClients.custom()
// 设置Cookie
.setDefaultCookieStore(cookieStore)
.build();
// 创建HttpPost对象
HttpPost httpPost = new HttpPost("http://localhost:8083/cookie");
// 设置超时
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(15000) // 设置连接超时时间,单位毫秒
.setSocketTimeout(15000) // 请求获取数据的超时时间,单位毫秒
.setConnectionRequestTimeout(15000) // 设置从connect Manager获取Connection超时时间,单位毫秒
.build();
httpPost.setConfig(requestConfig);
// 设置请求头信息
httpPost.setHeader("content-type", "application/json");
// 添加Json参数
JSONObject param = new JSONObject();
param.put("username", "admin");
param.put("password", "123456");
// 将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(), "utf-8");
httpPost.setEntity(entity);
// 执行Post请求
CloseableHttpResponse response = httpclient.execute(httpPost);
// 获取响应状态
System.out.println("获取响应状态: " + response.getStatusLine().getStatusCode());
// 获取响应结果
String result = EntityUtils.toString(response.getEntity(), "utf-8");
// 将返回的响应结果字符串转化成为Json对象
JSONObject resultJson = new JSONObject(result);
// 获取网页源码
System.out.println("获取网页源码:" + resultJson);
// 获取Cookie
ListcookieList = cookieStore.getCookies();
for (Cookie cookies : cookieList) {
System.out.println("Cookie: " + cookies.getName() + "; " + cookies.getValue());
}
// 关闭流和释放系统资源
response.close();
// 关闭客户端
httpclient.close();
}
}
2、运行结果:
本文分享自 AllTests软件测试 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有