C# httpclient中携带cookie

CookieContainer cookieContainer = new CookieContainer();//创建CookieContainer

Cookie cookie1 = new Cookie("pac_uid","0_8535fa0a41922","/","qq.com");

cookie1.Expires = DateTime.Now.AddDays(1);

CookieContainer.Add(cookie1);//添加第一个cookie

Cookie cookie2 = new Cookie("_qpsvr_localtk","0.6371368566953317","/","qq.com");

cookie2.Expires = DateTime.Now.AddDays(1);

CookieContainer.Add(cookie2);//添加第二个cookie

HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookieContainer;

// 创建HttpClient实例
HttpClient client = new HttpClient(handler);

// 创建FormData对象
List<KeyValuePair<string, string>> formData = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("iActivityId", "555463"),
new KeyValuePair<string, string>("iFlowId", "948190")
};

// 创建HttpContent对象,将FormData对象添加到其中
HttpContent httpContent = new FormUrlEncodedContent(formData);

// 发送POST请求
HttpResponseMessage response = client.PostAsync("https://comm.ams.game.qq.com/ams/ame/amesvr?ameVersion=0.3&sServiceType=nz&iActivityId=555463&sServiceDepartment=group_a&sSDID=f935b752be9e4fdfde32539f89bb4bb7&sMiloTag=AMS-MILO-555463-948190-o1731071838-1686797312745-AqWzbY&isXhrPost=true", httpContent).Result;

// 获取响应内容
string responseBody = response.Content.ReadAsStringAsync().Result;

 

//输出响应结果
responseBody = Regex.Unescape(responseBody);
Console.WriteLine(responseBody);

 
 
posted @ 2023-06-15 14:40  奇迹之耀  阅读(955)  评论(0编辑  收藏  举报