Java接口自动化测试-Post请求Cookie、Headers、参数的设置

本文详细介绍如何使用HttpClient进行POST请求,包括无参数、无Header的基本POST请求,以及携带参数、Header和Cookies的复杂POST请求实现。通过示例代码,读者可以学习如何设置请求参数、Header信息和管理Cookies。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:HttpClient实现基本的POST请求

1.1:Mock模拟一个Post接口

[
	{		
		"description":"这是Mock接口,带Header和参数的Demo",
		"request":{			
			"uri":"/PostDemo/withHeaders",
			"method":"post"
		},
		
		"response":{
			"json":{
				"Code":"Success",
				"Data":{
					"Link":"./locatin/xxx.jpg",
					"Message":"Mock模拟的带Header,参数信息的POST请求"
				}			
			}
		}			
	}
]

1.2:普通的没有参数和Header、Cookies的Post请求

	public static void main(String[] args) {

		try {
			// 创建Post对象
			HttpPost post = new HttpPost("http://localhost:8889/PostDemo/withHeaders");
			//创建client
			CloseableHttpClient client = HttpClients.createDefault();
			//执行post请求
			HttpResponse response = client.execute(post);
			// 将response对象转换成String类型
			String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(responseStr);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

2:HttpClient实现对携带参数、Header、Cookies信息的POST接口的访问

2.1:Mock实现一个带有Cookie、Header、参数的接口

[
	{		
		"description":"这是Mock接口,带Header和参数的Demo",
		"request":{			
			"uri":"/PostDemo/withHeaders",
			"method":"post",
			"headers":{
				"context-Type":"AABB",
				"Agent":"CCDD"
			},
			"cookies":{
				"SessionID":"11AABB22"
			},
			"forms":{
				"name":"Anndy",
				"age":"18"			
			}
		},
		
		"response":{
			"json":{
				"Code":"Success",
				"Data":{
					"Link":"./locatin/xxx.jpg",
					"Message":"Mock模拟的带Header,参数信息的POST请求"
				}			
			}
		}			
	}
]

2.2:代码实现对这个接口的访问

	public static void main(String[] args) {

		/**
		 * 携带Cookies、参数、Header的POST请求
		 */
		try {
			// 1:----设置传递参数----
			// 创建post对象
			HttpPost post = new HttpPost("http://localhost:8889/PostDemo/withHeaders");
			// 创建集合 添加参数
			List<NameValuePair> list = new LinkedList<>();
			BasicNameValuePair param1 = new BasicNameValuePair("name", "Anndy");
			BasicNameValuePair param2 = new BasicNameValuePair("age", "18");
			list.add(param1);
			list.add(param2);
			// 使用URL实体转换工具
	        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
	        post.setEntity(entityParam);

			// 2:----设置Header信息----
			// 设置Headers头信息
			post.addHeader("context-Type", "AABB");
			post.addHeader("Agent", "CCDD");
			post.addHeader("Connection", "keep-alive");
			post.addHeader("Content-Type", "application/x-www-form-urlencoded");

			// 3:----设置Cookie信息----
			// 创建CookieStore对象用来管理cookie
			CookieStore cookieStore = new BasicCookieStore();
			// new BasicClientCookie对象 用来注入cookie
			BasicClientCookie cookie = new BasicClientCookie("SessionID", "11AABB22");

			cookie.setDomain("localhost");// 设置cookie的作用域

			cookieStore.addCookie(cookie);// 将cookie添加到cookieStore中

			HttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

			// 执行Post请求
			HttpResponse response = client.execute(post);
			// 将response对象转换成String类型
			String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(responseStr);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

2.3:执行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值