更新1: 发布/获取标题的屏幕截图:http://imageshack.us/photo/my-images/826/getpost.png
我正在寻找网络请求的帮助。请求的目的是登录站点并检索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"];
}
我不确定这是否与重定向有关,因为在C#应用程序中,response-uri是“https://account.guildwars2.com/login?redirect_uri=/download”
响应标头Chrome
来自C#应用程序的响应
答案 0 :(得分:5)
默认情况下,HttpWebRequest
会自动遵循HTTP重定向响应,这会阻止您捕获原始响应中的Set-Cookie
标头。要禁用此行为,请在发送请求之前将AllowAutoRedirect
设置为false
:
req.AllowAutoRedirect = false;
答案 1 :(得分:0)
您可以尝试使用以下方式检索Cookie:
CookieContainer cookies = new CookieContainer();
WebResponse resp = req.GetResponse();
CookieCollection cookieCollection = cookies.GetCookies(request.RequestUri);