一、C#服务器端响应存储cookie
public IActionResult Test2()
{
Response.Cookies.Append("user","张三丰");
Response.Cookies.Append("pwd", "123");
return Content("输出cookie成功:张三丰");
}
二、C#发送Http请求,获取响应cookie
使用:CookieContainer
string url = "http://localhost:5082/cookie/test2";
var cookies = new CookieContainer();
var handler = new HttpClientHandler() { CookieContainer = cookies };
HttpClient client = new HttpClient(handler);
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("name", "abc");
FormUrlEncodedContent content = new FormUrlEncodedContent(dic);
content.Headers.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36");
HttpResponseMessage resp = client.PostAsync(url, content).Result;
//获取相应获取cookie
string cookie = cookies.GetCookieHeader(new Uri(url));
Console.WriteLine(cookie);//输出:user=%E5%BC%A0%E4%B8%89%E4%B8%B0; pwd=123
Console.WriteLine(resp.Content.ReadAsStringAsync().Result);
打印结果:
文章来源:https://www.toymoban.com/news/detail-787204.html
更多:文章来源地址https://www.toymoban.com/news/detail-787204.html
C# 发送带cookie的http请求_C#发送请求带cookie
C# Post提交formdata类型数据-.Net Core
C# 正则表达式使用_C# Regex使用整理
到了这里,关于C# 获取Http请求服务器响应的cookie的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!