C#总是获取空Cookie

时间:2019-02-06 13:16:21

标签: c# winforms

所以我总是尝试查看任何网站cookie,但是我得到的字符串为空

var cookieJar = new CookieContainer();
var client = new RestClient("https://server.com") {
    UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
};
client.CookieContainer = cookieJar;
var request = new RestRequest(Method.GET);
var cookie = client.CookieContainer.GetCookieHeader(new 
Uri("https://server.com"));
MessageBox.Show("" + cookie);

网站Cookie

1 个答案:

答案 0 :(得分:0)

您尚未发送任何获取Cookie的请求。将Execute方法调用添加到您的代码中

var request = new RestRequest(Method.GET);
client.Execute(request); //add this line
var cookie = client.CookieContainer.GetCookieHeader(new Uri("https://server.com"));