HTTPWebResponse生响应,使用反射 [英] HTTPWebResponse Raw Response, using Reflection

查看:193
本文介绍了HTTPWebResponse生响应,使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTPWebResponse为标题公开性质,是否有可能得到这样的,一旦我们开始使用插座使用,标题和内容相结合的原始响应反射,我认为必须有一种方式。

HTTPWebResponse exposes properties for Headers, is it possible to get raw response like the once we get using socket, Header and Content combined using Reflection, I think there must be a way.

我可以用插座,但大量的工作需要,使他们可以使用,如代理支持,HTTPS,进度事件,等等...清单很长,我一直强烈建议使用HttpWebRequest的,唯一的问题是我需要的原标题与响应,网站,我试图下载发送一个很长的和奇怪的cookie,它不是由HttpWebRequest的,Web客户端处理。字preSS博客,无法登录使用Web客户端的任何单词preSS博客,但接手动cookie处理,它可以完美运行,可能会在Web客户端中的错误。

I can use socket but a lot of work needed to make them usable, like proxy support, https, progress events, etc... list is long, i have been strongly advised to use HTTPWebRequest, only problem is i need the raw headers with the response, the websites i am trying to download sends a very long and weird cookie, which is not handled by HTTPWebRequest, WebClient. Wordpress blogs, not able to login any wordpress blog using WebClient, but with Sockets manual cookie handling it works perfect, may be a bug in WebClient.

1)只需要在原始消息头,这会做的伎俩。

2),也文章链接

2) And also article link

文章说HttpWebRequest的还有一个问题,只有一个线程下载,而其他人一直在等待,如果这是真的,那么套接字是更好??

The article says HTTPWebRequest there is a problem, only one thread is downloading while others are kept waiting, if this is true then sockets are better??

的文章说: 这code运作良好,但它有一个非常严重的问题,因为WebRequest类功能的GetResponse锁定访问所有其他进程中,WebRequest的告诉检索响应,关闭,因为在$ P $的最后一行pvious code。所以,我注意到,永远只有一个线程,而其他正在等待的GetResponse下载。为了解决这个严重的问题,我实现了我的两个班,采用Socket MyWebRequest和MyWebResponse。

The article says: This code works well but it has a very serious problem as the WebRequest class function GetResponse locks the access to all other processes, the WebRequest tells the retrieved response as closed, as in the last line in the previous code. So I noticed that always only one thread is downloading while others are waiting to GetResponse. To solve this serious problem, I implemented my two classes, MyWebRequest and MyWebResponse using Socket.

推荐答案

有一种方式来获得原始的标题:

There is a way to get raw headers:

var rawHeaders = request.GetResponse().Headers.ToString();

与您的网站,并要求您提供它返回:

With your website and request you provided it returned:

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

这是否解决问题了吗?

Does this solve your problem?

关于套接字,而不是WebRequests - 我不推荐这种做法。这是重新发明轮子。

About Sockets instead of WebRequests - I would recommend against that approach. It is reinventing the wheel.

更新

这并不解决问题,如上头已经分析了有损方式(详见注释)。经仔细检查我得出的结论,即原标题字节后 HttpWebRequest.GetResponse已经输了()

This does not solve the problem, as above headers are already parsed in a lossy way (see comments for details). Upon closer inspection I came to the conclusion, that raw header bytes are already lost after HttpWebRequest.GetResponse().

核心解析在完成System.Net.WebHeaderCollection.ParseHeaders() System.Net.WebHeaderCollection.ParseHeadersStrict()(根据 System.Net.Configuration.SettingsSectionInternal.Section.UseUnsafeHeaderParsing 的值),这两种方法不能记录所需的信息。不久之后,他们在操作缓冲( System.Net.Connection.m_ReadBuffer )充满了从导线新的数据。原头被丢失。

The core parsing is done in System.Net.WebHeaderCollection.ParseHeaders() or System.Net.WebHeaderCollection.ParseHeadersStrict() (depending on the value of System.Net.Configuration.SettingsSectionInternal.Section.UseUnsafeHeaderParsing) and both methods fail to record the required information. Soon after, the buffer they operate on (System.Net.Connection.m_ReadBuffer) is filled with new data from the wire. The original headers are lost.

为了保存原始数据,你需要重新实现 System.Net.Connection 类,它是内部和硬引用的的ServicePoint,这是公开的,但还是硬引用的HttpWebRequest的。综上所述,你将不得不重新实现整个堆栈。

In order to save the raw data, you would need to reimplement the System.Net.Connection class, which is internal and hard-referenced by ServicePoint, which is public, but still hard-referenced by HttpWebRequest. To sum up, you would have to reimplement the whole stack.

所以,除非你可以更改网站的行为或生活在没有这些cookie,你就需要使用一个插座。如果是那样的话,我想提出我的哀悼。

So unless you can change the website behavior or live without these cookies, you will need to use a Socket. If that's the case, I would like to offer my condolences.

这篇关于HTTPWebResponse生响应,使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆