一、C#服務(wù)器端響應(yīng)存儲(chǔ)cookie
public IActionResult Test2()
{
Response.Cookies.Append("user","張三豐");
Response.Cookies.Append("pwd", "123");
return Content("輸出cookie成功:張三豐");
}
?
二、C#發(fā)送Http請求,獲取響應(yīng)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;
//獲取相應(yīng)獲取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);
?打印結(jié)果:
?文章來源:http://www.zghlxwxcb.cn/news/detail-787204.html
更多:文章來源地址http://www.zghlxwxcb.cn/news/detail-787204.html
C# 發(fā)送帶cookie的http請求_C#發(fā)送請求帶cookie
C# Post提交formdata類型數(shù)據(jù)-.Net Core
C# 正則表達(dá)式使用_C# Regex使用整理
到了這里,關(guān)于C# 獲取Http請求服務(wù)器響應(yīng)的cookie的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!