来自 HttpWebRequest 的 CookieContainer 具有直接指向文件的路径 - 如何忽略路径?
CookieContainer from HttpWebRequest has path directly to file - How to ignoring path?
我的软件中有一个错误,因为 C# HttpWebRequest 仅将域的 cookie 用于直接文件。
例如:
具有值 test
的 Cookie XYZ
具有域 127.0.0.1
的路径 /index.html
。
例如,我想将此 cookie 与路径 /
一起使用。
我该怎么做?
当前方法:
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create("http://127.0.0.1/index.html") as HttpWebRequest;
request.CookieContainer = cc;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();
// No cookies would be sent!
request = WebRequest.Create("http://127.0.0.1/informations.html") as HttpWebRequest;
request.CookieContainer = cc; // cc contains a cookie with path `/index.html` which wouldnt sent to informations.html
response = request.GetResponse() as HttpWebResponse;
sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();
提前致谢!
按照这种方式应该可行:
此代码使用从初始请求接收的数据填充 cookie 容器。 Cookie 是为整个域设置的,在根路径上,因此它们应该在每次请求时发送。
你能查一下吗?
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create("http://127.0.0.1/index.html") as HttpWebRequest;
//request.CookieContainer = cc;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
foreach (Cookie cookie in response.Cookies)
{
Cookie newCookie = new Cookie(cookie.Name, cookie.Value, "/", "127.0.0.1");
cc.Add(new Uri("http://127.0.0.1"), cookie);
}
StreamReader sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();
我的软件中有一个错误,因为 C# HttpWebRequest 仅将域的 cookie 用于直接文件。
例如:
具有值 test
的 Cookie XYZ
具有域 127.0.0.1
的路径 /index.html
。
例如,我想将此 cookie 与路径 /
一起使用。
我该怎么做?
当前方法:
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create("http://127.0.0.1/index.html") as HttpWebRequest;
request.CookieContainer = cc;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();
// No cookies would be sent!
request = WebRequest.Create("http://127.0.0.1/informations.html") as HttpWebRequest;
request.CookieContainer = cc; // cc contains a cookie with path `/index.html` which wouldnt sent to informations.html
response = request.GetResponse() as HttpWebResponse;
sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();
提前致谢!
按照这种方式应该可行:
此代码使用从初始请求接收的数据填充 cookie 容器。 Cookie 是为整个域设置的,在根路径上,因此它们应该在每次请求时发送。
你能查一下吗?
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create("http://127.0.0.1/index.html") as HttpWebRequest;
//request.CookieContainer = cc;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
foreach (Cookie cookie in response.Cookies)
{
Cookie newCookie = new Cookie(cookie.Name, cookie.Value, "/", "127.0.0.1");
cc.Add(new Uri("http://127.0.0.1"), cookie);
}
StreamReader sr = new StreamReader(response.GetResponseStream());
MessageBox.Show(sr.ReadToEnd());
sr.Close();
response.Close();