C# HttpWebRequest resp.Headers[";Set Cookie";]为空
更新1: 发布/获取标题的屏幕截图: 我正在寻求有关web请求的帮助。请求的目的是登录到站点并检索设置的Cookie头。我很感激你的帮助 我正在尝试登录到一个站点,并使用下面的代码检索cookie值。但是,Set Cookie标头始终为空:C# HttpWebRequest resp.Headers[";Set Cookie";]为空,c#,cookies,webrequest,C#,Cookies,Webrequest,更新1: 发布/获取标题的屏幕截图: 我正在寻求有关web请求的帮助。请求的目的是登录到站点并检索设置的Cookie头。我很感激你的帮助 我正在尝试登录到一个站点,并使用下面的代码检索cookie值。但是,Set Cookie标头始终为空: static void Main(string[] args) { string formUrl = "https://account.guildwars2.com/login"; string formParams = string.For
static void Main(string[] args)
{
string formUrl = "https://account.guildwars2.com/login";
string formParams = string.Format("email={0}&password={1}", "xxx", "xxx");
string cookieHeader;
var req = (HttpWebRequest)WebRequest.Create(formUrl);
req.Referer = "https://account.guildwars2.com/login";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.KeepAlive = true;
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-Cookie"];
}
我不确定这是否与重定向有关,因为在C#应用程序中,响应uri为“”
响应标题Chrome
- 内容长度:0
- 内容类型:text/html
- 日期:2012年9月29日星期六格林尼治标准时间15:07:30
- 地点:下载
- 服务器:Microsoft IIS/7.5
- 设置Cookie:s=60B845F0-4E22-4A4B-890B-F3B885CEF9AE;Domain=guildwars2.com;路径=/;版本=1
- X供电:ARR/2.5
- 内容长度:7344
- 内容类型:text/html
- 日期:2012年9月29日星期六15:42:44 GMT
- 服务器:Microsoft IIS/7.5
- X供电:ARR/2.5
CookieContainer cookies = new CookieContainer();
WebResponse resp = req.GetResponse();
CookieCollection cookieCollection = cookies.GetCookies(request.RequestUri);
您可以使用以下方法尝试检索cookie:
CookieContainer cookies = new CookieContainer();
WebResponse resp = req.GetResponse();
CookieCollection cookieCollection = cookies.GetCookies(request.RequestUri);
默认情况下,
HttpWebRequest
会自动跟随HTTP重定向响应,这会阻止您在原始响应中捕获Set Cookie
头。要禁用此行为,请在发送请求之前设置为false
:
req.AllowAutoRedirect = false;
默认情况下,
HttpWebRequest
会自动跟随HTTP重定向响应,这会阻止您在原始响应中捕获Set Cookie
头。要禁用此行为,请在发送请求之前设置为false
:
req.AllowAutoRedirect = false;
我认为您需要在发送之前将httpWebRequest.CookieCollection设置为new CookieCollection,可能在不使用VS atm的情况下键入错误。我尝试了req.CookieContainer=new CookieContainer();但是没有添加任何内容。我认为您需要在发送之前将httpWebRequest.CookieCollection设置为new CookieCollection,可能在不使用VS atm时输入错误。我尝试了req.CookieContainer=new CookieContainer();但是没有任何东西被添加到hanks中,只有空的cookie集合。我开始认为这与重定向有关。当我在google中调试时,它首先以状态303(重定向)发布到登录站点,然后以状态200(确定)获取下载站点。谢谢,但是cookie集合是空的。我开始认为这与重定向有关。当我在google中调试时,它首先以状态303(重定向)发布到登录站点,然后以状态200(确定)获取下载站点。