Search code examples
c#httpwebrequest

Diagnostic dump of HttpWebRequest object


Is there any good way (other than tediously querying each property) to build a diagnostic dump string for an HttpWebRequest in C#? For simple objects one can get away with using new JObject(theObject), but that doesn't work for HttpWebRequest (and toString is, of course, as useless on HttpWebRequest as it is on any C# object).

Is there any general way to do this for any subset of C# objects (other than using new JObject as I already mentioned)?

Update: I've found that using JsonConvert.SerializeObject (one suggestion in Hans' link) does a decent job of dumping the HttpWebRequest (though it doesn't get the request stream). And I kind of got the System.Net.Tracing stuff to work (though the documentation, as usual for .NET stuff, sucks royally).


Solution

  • This turns out to work pretty well:

    string httpWebRequestDump(HttpWebRequest hwr)
    {
        return JsonConvert.SerializeObject(hwr,Formatting.Indented);
    }
    

    The only problem is that it doesn't dump the associated request stream. I have not found a way to extract that from the request.