IUnknown
IUnknown

Reputation: 2636

ETag not being returned by WebResponse Header in c#

I am trying to extract the ETag from the response header. It does exist in the response. I can see it using firebug and I can see it in the response object using the inspector:

Status: 200 OK
X-Api-Version: 1.3.2
Access-Control-Allow-Origin: *
X-Runtime: 0.151298
Connection: keep-alive
Content-Length: 8185
Cache-Control: public, max-age=11216
Content-Type: application/json; charset=utf-8
Date: Fri, 09 Mar 2012 01:40:05 GMT
Expires: Fri, 09 Mar 2012 04:47:01 GMT
ETag: "bd3fe1123a8f55e01ca859f4804e8fbe"
Last-Modified: Fri, 09 Mar 2012 00:47:01 GMT
Server: nginx/1.0.11

All the other code is working fine, making the HttpWebRequest, getting the respose etc. The only problem is I always get null when trying to get the ETag (which does existing in the response header).

Here is the simplified code:

        var request = (HttpWebRequest)WebRequest.Create(validUri);
        SetHeaders(); // helper function to set basic headers.
        var response = request.GetResponse();

        var stream = response.GetResponseStream();
        var reader = new StreamReader(stream);
        var result = reader.ReadToEnd();

        var etag = response.Headers.Get("ETag");

Anyone know why I can't seem to extract the existing ETag?

Upvotes: 1

Views: 3371

Answers (1)

Andrew Savinykh
Andrew Savinykh

Reputation: 26270

Your code does not reproduce the problem you are describing. Your problem is in something that you have not mentioned. Here is a short complete program based on your code, that executes and does print out the value of ETag without a problem:

using System;
using System.IO;
using System.Net;

namespace SO9628006
{
    class Program
    {
        static void Main()
        {
            var request = (HttpWebRequest)WebRequest.Create("http://www.fiddler2.com/Fiddler/Fiddler.css");
            var response = request.GetResponse();

            var stream = response.GetResponseStream();
            var reader = new StreamReader(stream);
            var result = reader.ReadToEnd();

            var etag = response.Headers.Get("ETag");

            Console.WriteLine(etag);

        }
    }
}

Output:

"6c3673ba919ec71:243"

Could you please provide short but complete program that illustrates your issue?

Upvotes: 2

Related Questions