C# 获取Http请求服务器响应的cookie

一、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);

 打印结果:

 

更多:

C# 发送带cookie的http请求_C#发送请求带cookie

C# Post提交formdata类型数据-.Net Core

C# 正则表达式使用_C# Regex使用整理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值