简体   繁体   English

如何设置HttpWebResponse的内容长度限制

[英]How to Set Limit on Content Length for HttpWebResponse

When I use: 我用的时候:

   response = (HttpWebResponse)req.GetResponse();

it will respond with the package content of the entire HTML from the webpage. 它将响应来自网页的整个HTML的包内容。 But I only want to get the header of the webpage. 但我只想得到网页的标题。 Can I set a limit for response content length? 我可以设置响应内容长度的限制吗?

Thanks you! 谢谢!

If you just need HTTP header - use HEAD request instead of GET. 如果您只需要HTTP标头 - 请使用HEAD请求而不是GET。 If you need some part of a web page it may be easier to read whole response unless you know what part of it you need. 如果您需要网页的某些部分,则可能更容易阅读整个响应,除非您知道它需要哪个部分。 You can either read part of response stream or use range - HttpWebRequest.AddRange . 您可以读取部分响应流或使用范围 - HttpWebRequest.AddRange

Use the Headers property on the response object. 使用响应对象上的Headers属性。 Just calling GetResponse() doesn't pull in the whole thing until you start reading it. 在你开始阅读它之前,只是调用GetResponse()并不会GetResponse()整个事情。

http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.headers.aspx http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.headers.aspx

Code taken from the site above: 从以上网站获取的代码:

            // Creates an HttpWebRequest for the specified URL. 
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
            // Sends the HttpWebRequest and waits for response.
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

            // Displays all the headers present in the response received from the URI.
            Console.WriteLine("\r\nThe following headers were received in the response:");
            // Displays each header and it's key associated with the response.
            for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)  
                Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); 
            // Releases the resources of the response.
            myHttpWebResponse.Close(); 

I think the examples on this page cover what you need. 我认为此页面上的示例涵盖了您的需求。

Example: 例:

// Creates an HttpWebRequest for the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 

// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 

// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");

// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)  
Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); 

// Releases the resources of the response.
myHttpWebResponse.Close(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM