【问题标题】:.NET HttpWebResponse not providing Location header.NET HttpWebResponse 不提供 Location 标头
【发布时间】:2011-01-13 18:25:00
【问题描述】:

我在从 WebResponse 获取 Location 标头时遇到问题:

private CookieContainer _cookieContainer = new CookieContainer();
...
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);

webRequest.Method = "POST";          
webRequest.Referer = "www.xxxxx.sk";            
webRequest.Proxy = GetSystemProxy();
webRequest.AllowAutoRedirect = false;
webRequest.CookieContainer = _cookieContainer;
webRequest.ContentType = "application/x-www-form-urlencoded";                                
webRequest.KeepAlive = false;            
//webRequest.Expect = "Location";

当我在 Firefox 中使用 Tamper data 插件时 - 我得到了我想要的:

位置=http://www.xxxxx.sk?i9=3522a42d0207

但是查看HttpWebResponse.Headers 不包含此标头(尽管收到的标头中有 90% 与 firefox 的情况相同)

我尝试设置 .Expect 以强制服务器向我提供该标头,但出现错误 417 预期失败。

我想要该标头的原因是,其中有一个会话 ID,我确实需要进一步请求(网站具有双重安全性 - URL 中的 cookie + 会话 ID)

我不明白的是,默认情况下HttpWebRequest 将遵循重定向,因此如果服务器发送 301/302 状态代码,将发出一个新请求以使用 Location 标头获取资源.因此,一旦获取此最终资源,响应中将不再有 Location 标头。但是如果我将AllowAutoRedirect设置为false,为什么header没有出现?

已编辑:标题:

Mozilla request headers:


Host=prihlasenie.azet.sk
User-Agent=Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language=en-us,en;q=0.5
Accept-Encoding=gzip,deflate
Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive=115
Connection=keep-alive
Referer=http://www.azet.sk/
Content-Type=application/x-www-form-urlencoded
Content-Length=64
POSTDATA=form%5Busername%5D=yyyyyyyyyy&form%5Bpassword%5D=zzzzzzzzzz
--
Mozilla response headers:



Status=Found - 302
Date=Fri, 14 Jan 2011 13:12:37 GMT
Server=Apache
Pragma=no-cache
Expires=Fri, 14 Jan 2011 13:12:37 +0000
Cache-Control=post-check=0, pre-check=0, no-cache, must-revalidate
Set-Cookie=AZetSecId=3a2f118910; expires=Fri, 28-Jan-2011 13:12:37 GMT; path=/; domain=.azet.sk
Location=http://www.azet.sk/?i9=6ffcf488a877
Vary=User-Agent,Accept-Encoding
X-Served-By=prihlasenieweb-04
Content-Encoding=gzip
Content-Length=1124
Keep-Alive=timeout=15, max=82
Connection=Keep-Alive
Content-Type=text/html

--
------------------------------------------------
---
HttpWebRequest headers:

{Referer: http://www.xxxx.sk
Content-Type: application/x-www-form-urlencoded
Host: prihlasenie.xxxx.sk
Content-Length: 59
Expect: 100-continue
Connection: Close
}

and I'm doing:

byte[] paramBytes = Encoding.ASCII.GetBytes(postParameters);
webRequest.ContentLength = paramBytes.Length;

using (Stream requestStream = webRequest.GetRequestStream())
{
    requestStream.Write(paramBytes, 0, paramBytes.Length);
}
---
HttpWebResponse headers:

{Pragma: no-cache
Vary: User-Agent,Accept-Encoding
X-Served-By: prihlasenieweb-05
Connection: close
Content-Length: 2113
Cache-Control: post-check=0, pre-check=0, no-cache, must-revalidate
Content-Type: text/html
Date: Thu, 13 Jan 2011 20:56:21 GMT
Expires: Thu, 13 Jan 2011 20:56:21 +0000
Set-Cookie: xxxxSecId=b425262c2e; expires=Thu, 27-Jan-2011 20:56:21 GMT; path=/; domain=.xxxx.sk
Server: Apache
}

【问题讨论】:

  • 你为什么要使用“Expect”标题?另一种选择是 cookieContainer 在请求之前未正确初始化。
  • 嗯。听起来像一个错误。您可以获取 system.net 跟踪日志并将其放在 pastebin 上吗?以下是说明ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html
  • 我不是有意正常使用Expect,但值得一试..
  • 为什么你认为这是一个错误?无论如何,我可以嗅出从我的机器发出的数据包并将 http 标头与 firefox 的标头进行比较..
  • @PaN1C_Showt1Me 好吧,关键是:您需要任何形式的身份验证吗?如果是这样,问题将是会话(因为它是空的)。当“期望”不存在时,服务器会返回什么。 (标题有完全不同的目的,417 是一个很好的结果)。发布带有 HttpWebRequest 的 301/302 的标头

标签: header httpwebrequest http-headers httpwebresponse


【解决方案1】:

问题在于 cookie 的初始化,因为网络服务器可能依赖它们来启动身份验证过程。

额外说明:请在标签中添加(http)cookie(“http-headers”覆盖它,但它也是一个全局标签)

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2016-12-25
    • 2017-02-17
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多