1
byte[] jData = Encoding.UTF8.GetBytes(DATA);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });

//required this (header method not working)
CredentialCache crCache = new CredentialCache();
crCache.Add(new Uri(URL), "Basic", new NetworkCredential(uName, uPwd));
req.Credentials = crCache;

//required below, to enable cookies to carry authentication value over each request.
req.CookieContainer = new CookieContainer();

req.Accept = "application/json";
req.Method = "POST";
req.ContentType = "application/json;charset=utf-8";
req.Headers.Add(headerKey, headerValue);

req.GetRequestStream().Write(jData, 0, jData.Length);

using (HttpWebResponse res = (HttpWebResponse)req.GetResponse()) {
    using (StreamReader strm = new StreamReader(res.GetResponseStream())) {
        result = strm.ReadToEnd();
    }
}

The above lines of code is a simple call to JSon Web service. The issue is the call is failing after sometime with UnAuthorized error. So I need to log HttpWeRequest to a text file to see the entire request. This should include headers, authentication credentials and body.. Can I know how can the entire request be captured and written to a file.

Thanks

stuartd
  • 70,509
  • 14
  • 132
  • 163
AndyKash
  • 57
  • 2
  • 8
  • First, you can reduce all this code to just 3-4 lines with HttpClient. Second, you can capture requests and responses easily using a debugging proxy like Fiddler. – Panagiotis Kanavos Nov 12 '20 at 18:11
  • Thanks for the reply @kanavos, the need is to log the request within the .NET framework. Cannot use wireshark or Fiddler – AndyKash Nov 12 '20 at 18:19
  • You can [enable network tracing](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing) but that's a bit verbose. Or you can create and [use a logging HttpHandler with HttpClient](https://www.stevejgordon.co.uk/httpclientfactory-asp-net-core-logging). Which you should be using anyway. In fact, if you're in .NET Core 3+ you're already using it indirectly, as HttpWebRequest was reimplemented as a legacy wrapper on top of HttpClient – Panagiotis Kanavos Nov 13 '20 at 07:20
  • The answers to the duplicate describe both techniques - a logging HttpClientHandler and enabling network tracing – Panagiotis Kanavos Nov 13 '20 at 07:23

2 Answers2

3
for (int i = 0; i < req.Headers.Count; ++i)
{
    string header = req.Headers.GetKey(i);
    foreach (string value in req.Headers.GetValues(i))
    {
        Console.WriteLine("{0}: {1}", header, value);
    }
}
  • Thanks for this suggestion, this partly works and gives the header information. Wanted to know if we can recreate the entire request as is that's sent to clint. – AndyKash Nov 12 '20 at 19:49
1

GetResponseStream() gives a stream of the response body no the whole response

You can use Header property of HttpWebResponse to catch headers

https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.headers?view=net-5.0#System_Net_HttpWebResponse_Headers

qazwsx123
  • 161
  • 1
  • 11