Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置HttpWebResponse的内容长度限制_C#_.net_Httpwebrequest_Httpwebresponse - Fatal编程技术网

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

C# 如何设置HttpWebResponse的内容长度限制,c#,.net,httpwebrequest,httpwebresponse,C#,.net,Httpwebrequest,Httpwebresponse,当我使用: response = (HttpWebResponse)req.GetResponse(); 它将响应网页中整个HTML的包内容。但我只想得到网页的标题。我可以设置响应内容长度的限制吗 谢谢你 如果您只需要HTTP头,请使用头请求而不是GET。如果您需要某个网页的某个部分,可能更容易阅读整个响应,除非您知道您需要的部分。您可以读取响应流的一部分,也可以使用范围-。使用响应对象上的Headers属性。只需调用GetResponse(),在您开始阅读之前,不会将整个内容都包含在内

当我使用:

   response = (HttpWebResponse)req.GetResponse();
它将响应网页中整个HTML的包内容。但我只想得到网页的标题。我可以设置响应内容长度的限制吗


谢谢你

如果您只需要HTTP头,请使用头请求而不是GET。如果您需要某个网页的某个部分,可能更容易阅读整个响应,除非您知道您需要的部分。您可以读取响应流的一部分,也可以使用范围-。

使用响应对象上的
Headers
属性。只需调用
GetResponse()
,在您开始阅读之前,不会将整个内容都包含在内

代码取自上述站点:

            // 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(); 
//为指定的URL创建HttpWebRequest。
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
//发送HttpWebRequest并等待响应。
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
//显示从URI接收的响应中存在的所有标头。
Console.WriteLine(“\r\n在响应中接收到以下标题:”);
//显示每个标题及其与响应关联的键。
对于(int i=0;i
我认为上面的示例涵盖了您需要的内容

例如:

// 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(); 
//为指定的URL创建HttpWebRequest。
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
//发送HttpWebRequest并等待响应。
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
//显示从URI接收的响应中存在的所有标头。
Console.WriteLine(“\r\n在响应中接收到以下标题:”);
//显示每个标题及其与响应关联的键。
对于(int i=0;i