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
来自C#应用程序的响应
默认情况下,会HttpWebRequest
自动遵循HTTP重定向响应,这会阻止您捕获Set-Cookie
原始响应中的标头.要禁用此行为,AllowAutoRedirect
请false
在发送请求之前设置为:
req.AllowAutoRedirect = false;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5327 次 |
最近记录: |