首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >HttpClient-Header、Cookie

HttpClient-Header、Cookie

作者头像
wangmcn
发布2022-07-25 17:12:32
发布2022-07-25 17:12:32
8360
举报
文章被收录于专栏:AllTests软件测试AllTests软件测试

Header、Cookie

目录

  • 1、Header
  • 2、Cookie

1、Header

1、创建Headers类。

添加2个Header:

内容格式设定为Json格式("content-type", "application/json")、自定义Header("Self-Header", "MySelfHeader")。

脚本代码:

代码语言:javascript
复制
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。

脚本代码:

代码语言:javascript
复制
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、运行结果:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AllTests软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档