I have a Tuple that contains HTTP headers and their values. This portion of my code is using the Content-Encoding: header to work out if the content is compressed with gzip or deflate.
var contentEncoding = responseHeaders.Find(p => p.Item1.ToLower() == "content-encoding");
// decompress the content if needed
if (!String.IsNullOrEmpty(contentEncoding.Item2))
if (contentEncoding.Item2.ToLower() == "gzip")
bodyPlain = Tools.Gunzip(ret);
else if (contentEncoding.Item2.ToLower() == "deflate")
bodyPlain = Tools.Decompress(ret);
else
bodyPlain = UTF8Encoding.UTF8.GetString(ret);
else
bodyPlain = UTF8Encoding.UTF8.GetString(ret);
My problem is that if responseHeaders.Find does not return a result, then contentEncoding.Item2 does not exist and I get
Object reference not set to an instance of an object.
I tried checking contentEncoding.Item2 with String.IsNullOrEmpty and !=null, but it still returns the above error.
I also tried specifying contentEncoding before using it, like this:
var contentEncoding = new Tuple<string,string>("","");
contentEncoding = responseHeaders.Find(p => p.Item1.ToLower() == "content-encoding");
But still get the same error.
What am I doing wrong here, and is there a better way of achieving this?