C # HTTP Post and only response headers for the response before retrieving the body

I am doing something like this:

 var httpWebRequest = WebRequest.Create(context.Url) as HttpWebRequest;
 httpWebRequest.Method = "POST"
 ... (set all the stuff)
 ... (get request stream and post data)

 //Get response
 var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

 ... (Inspect Headers)

 //Get response stream and read body
 var responseStream = httpWebRequest.GetResponseStream();

According to my modest expectations, I thought that the call to GetResponse () would only retrieve the headers, and the body would be loaded when I started reading from the response stream. What actually happens is that when I call GetResponseStream () and read it, the data is already available. The answer is a regular HTML page. I believe that using data with channels it works well.

So my question is: what really happens there and how to get only the headers from the http message before receiving the body content?

+3
source share
2 answers

GET POST "" . , httpWebRequest.Method "HEAD" httpWebResponse.Headers(http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.headers.aspx) .

+2

:

 url = "some web string"
 uri = new UriBuilder(url).Uri;
 request = WebRequest.Create(this.uri);
 request.Method = "HEAD";
 response = request.GetResponse();
 response.Close();

. ! :

for (int i = 0; i < response.Headers.Count; ++i) {
    Console.WriteLine("\n Header Name:{0}, Value :{1}", response.Headers.Keys[i], response.Headers[i]); 
}

, , . , -, .

: Execpt , -, . response.ContentType.

0

All Articles