开发者

System.Net.HttpWebResponse Returning System.IO.Stream.NullStream

I have a case where the HttpWebResponse.GetResponseStream() returns a System.Net.NullStream even though examination of the HttpWebResponse object reveals that its underlying m_ConnectStream is an instance of System.Net.ConnectStream and the ContentL开发者_高级运维ength property matches exactly the length of the content returned from the server. I also poked around in the Watch window and found my data but can't remember where I found it, but I KNOW my response data is there, the runtime just won't let me at it!

The only thing that is different from other successful scenarios is that the HttpWebRequest verb is "HEAD". I'm implementing a highly RESTful web service and wanting to use "HEAD" to request metadata for resources.


Figured it out:

Found the following .Net Fx Source Code (in HttpWebResponse Class):

/// <devdoc> 
/// <para>Gets the stream used for reading the body of the response from the 
/// server.</para> 
/// </devdoc> 
public override Stream GetResponseStream() 
{ 
    if (Logging.On) 
        Logging.Enter(Logging.Web, this, "GetResponseStream", ""); 
    CheckDisposed(); 

    if (!CanGetResponseStream()) { 
        // give a blank stream in the HEAD case, which = 0 bytes of data 
        if (Logging.On) 
            Logging.Exit(Logging.Web, this, "GetResponseStream", 
                Stream.Null); 
        return Stream.Null; 
    } 
    if (Logging.On) 
        Logging.PrintInfo(Logging.Web, 
            "ContentLength=" + m_ContentLength); 
    if (Logging.On) 
        Logging.Exit(Logging.Web, this, "GetResponseStream", 
            m_ConnectStream); 
    return m_ConnectStream; 
} 

As you can see it explicitly returns a null stream for "HEAD" requests. "Why would it do that?" I ask.

I found this at http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html:

9.4 HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

Wow. I took from the Richardson and Ruby RESTful Web Services book that you might be clever and respond to a "HEAD" request with a blank XHTML Form that would fully describe the structure of a resource's elements including requiredness, datatype, length etc. using all of the XHTML(5) form field attributes. After reading the HTTP spec, however, it is clear that all 'HEAD' response data has to go in the HTTP headers.

Oh well, you learn something new everyday ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜