2011-01-13 79 views
4

我了WebResponse获得Location头一个问题:.NET HttpWebResponse不提供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中使用篡改数据的插件 - 我得到了我想要的:

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

但看HttpWebResponse.Headers不包含此标头(尽管90%的接收的头的是相同的,在Firefox的情况下)

我试过设置。 Expect强制服务器提供我的头,但我得到错误417期望失败。

为什么我要那个标题是,有一个会话ID的话,我确实需要进一步的请求的原因(网站具有双重安全 - 饼干+会话ID在URL)

我也不是什么明白是默认HttpWebRequest将遵循重定向,所以如果服务器发送301/302状态代码将发出一个新的请求来获取资源使用位置标题。所以一旦获取了这个最终资源,响应中将不再有Location标头。但是,如果我将AllowAutoRedirect设置为false,为什么不显示标题?

编辑:头:

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 
} 
+0

你为何用“期待”头呢?另一种选择是cookieContainer在请求前未正确初始化。 – bestsss 2011-01-13 18:39:38

回答

2

问题的cookie的初始化,因为web服务器可以依靠他们开始认证过程中的规定。

额外注:在标签请加(HTTP)的cookie(“HTTP报头”一个涵盖它,但它太全局变量)