gpt4 book ai didi

c# - WebResponse 不返回任何 cookie

转载 作者:行者123 更新时间:2023-11-30 19:41:52 48 4
gpt4 key购买 nike

我目前正在研究一个帮助我创建带有 cookie 和 POST 参数的 WebRequest 的类。但是 response.cookies 总是空的。我如何获取返回的 cookie 并将它们与下一个请求一起发送?

class WebHandler
{
private string lasturl = "";
private Dictionary<string, Cookie> cookies;
public string lastContent;

public WebHandler()
{
cookies = new Dictionary<string, Cookie>();
}

public HttpWebResponse request(string address)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "GET";
if(lasturl != "")
request.Referer = lasturl;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}

public HttpWebResponse request(string address, Dictionary<string, string> postParameters)
{
lasturl = address;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}
request.Method = "POST";

string postData = "";
foreach (string key in postParameters.Keys)
postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postParameters[key]) + "&";


byte[] data = Encoding.ASCII.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie newCookie in response.Cookies)
{
Console.WriteLine(" new cookie: " + newCookie.Name + " = " + newCookie.Value);
this.cookies[newCookie.Name] = newCookie;
}
lastContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
return response;
}
}

最佳答案

因为 this.cookies 最初是空的,你的代码永远不会进入循环

foreach (KeyValuePair<string, Cookie> pair in this.cookies)
{
Console.WriteLine(" Sent cookie: " + pair.Value.Name + " = " + pair.Value.Value);
request.CookieContainer.Add(pair.Value);
}

它的坏部分是您永远不会看到 request.CookieContainer 为 null(否则您会得到 null 引用异常),这会告诉您有问题。

由于 request.CookieContainer 为空,HttpWebResponse 永远不会获取返回的 cookie。

解决方案:简单,只需添加

if (request.CookieContainer == null) 
request.CookieContainer = new CookieContainer();

之后

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

PS:您可能想阅读 this question关于 c# 中的命名约定。

PS2:在方法 request 中使用变量名 request 不会提高可读性。

关于c# - WebResponse 不返回任何 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449180/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com