有一种方法可以获取原始标头:
var rawHeaders = request.GetResponse().Headers.ToString();
使用您的网站并请求您提供它返回:
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Cache-Control: no-cache, must-revalidate, max-age=0
Date: Wed, 03 Aug 2011 12:08:49 GMT
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/,wordpress_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/wp-admin,wordpress_sec_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/wp-admin,wordpress_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/wp-content/plugins,wordpress_sec_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/wp-content/plugins,wordpress_logged_in_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpress_logged_in_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpress_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpress_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpress_sec_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpress_sec_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpressuser_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpresspass_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpressuser_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/,wordpresspass_c2d1208bd3bc2294298da94d67693495=+; expires=Tue, 03-Aug-2010 12:08:49 GMT; path=/
Server: Apache
X-Powered-By: PHP/5.2.17
Last-Modified: Wed, 03 Aug 2011 12:08:49 GMT
Content-Type: text/html; charset=UTF-8
X-Cache: MISS from localhost
X-Cache-Lookup: MISS from localhost:3128
Via: 1.0 localhost (squid/3.1.6)
Connection: close
这能解决您的问题吗?
关于 Sockets 而不是 WebRequests - 我建议不要使用这种方法。它正在重新发明轮子。
更新
这并不能解决问题,因为上面的标头已经以有损方式进行了解析(有关详细信息,请参阅 cmets)。经过仔细检查,我得出结论,在HttpWebRequest.GetResponse() 之后,原始标头字节已经丢失。
核心解析在System.Net.WebHeaderCollection.ParseHeaders()或System.Net.WebHeaderCollection.ParseHeadersStrict()(取决于System.Net.Configuration.SettingsSectionInternal.Section.UseUnsafeHeaderParsing的值)中完成,两种方法都无法记录所需的信息。不久之后,他们操作的缓冲区 (System.Net.Connection.m_ReadBuffer) 被来自网络的新数据填充。原始标题丢失了。
为了保存原始数据,您需要重新实现 System.Net.Connection 类,该类是内部的并且由 ServicePoint 硬引用,它是公共的,但仍由 HttpWebRequest 硬引用。总而言之,您必须重新实现整个堆栈。
因此,除非您可以更改网站行为或在没有这些 cookie 的情况下生活,否则您将需要使用 Socket。如果是这样,我想表示哀悼。