HttpWebRequest resp.Headers ["Set-Cookie"]为空

Ska*_*lis 1 c# cookies webrequest

更新1:发布/获取标题的屏幕截图:http://imageshack.us/photo/my-images/826/getpost.png

我正在寻求Web请求的帮助.请求的目的是登录站点并检索Set-Cookie标头.我很感激任何帮助!

我对网络请求有点新意,所以请跟我一起玩.我正在尝试使用以下代码登录网站并检索cookie值.但是,Set-Cookie标头始终为null:

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"];
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是否与重定向有关,因为在C#应用程序中,response-uri是"https://account.guildwars2.com/login?redirect_uri=/download"

响应标头Chrome

  • 内容长度:0
  • 内容类型:text/html的
  • 日期:星期六,2012年9月29日15:07:30 GMT
  • 地点:下载
  • 服务器:Microsoft-IIS/7.5
  • 设置Cookie:S = 60B845F0-4E22-4A4B-890B-F3B885CEF9AE; 域= guildwars2.com; 路径= /; 版本= 1
  • X-已启动方式:ARR/2.5

来自C#应用程序的响应

  • 内容长度:7344
  • 内容类型:text/html
  • 日期:星期六,2012年9月29日15:42:44 GMT
  • 服务器:Microsoft-IIS/7.5
  • X-Powered-By:ARR/2.5

Mic*_*Liu 5

默认情况下,会HttpWebRequest自动遵循HTTP重定向响应,这会阻止您捕获Set-Cookie原始响应中的标头.要禁用此行为,AllowAutoRedirectfalse在发送请求之前设置为:

req.AllowAutoRedirect = false;
Run Code Online (Sandbox Code Playgroud)