views:

54

answers:

1

How can i see HttpWebRequest object as string before calling GetResponse method? I want to see raw format of request something like this as in fiddler:

Content-Type: multipart/form-data; boundary=---------------------------2600251021003 
Content-Length: 338 
-----------------------------2600251021003 Content-Disposition: form-data; name="UPLOAD_FILEName"; filename="Searchlight062210 w price.csv" Content-Type: application/vnd.ms-excel 
,,,,, 
-----------------------------2600251021003 
Content-Disposition: form-data; name="submit" 
submit 
-----------------------------2600251021003-- 

I tried following code, but not worked because stream is not readable.

 string GetRequestString(HttpWebRequest req)
        {
            Stream stream2 = req.GetRequestStream(); 
            StreamReader reader2 = new StreamReader(stream2);
            return reader2.ReadToEnd();  

        }
+2  A: 

If it is for logging purposes you could activate tracing by putting this in your app/web.config:

  <system.diagnostics>
    <sources>
      <source name="System.Net.Sockets" tracemode="protocolonly">
        <listeners>
          <add name="System.Net.Sockets" type="System.Diagnostics.TextWriterTraceListener" initializeData="network.log" />
        </listeners>
      </source>
    </sources>

    <switches>
      <add name="System.Net.Sockets" value="Verbose"/>
    </switches>

    <trace autoflush="true" />
  </system.diagnostics>

Run your code and look at the generated log file.

Darin Dimitrov
tracemode="protocolonly" in app.config is not recognized
Brij
@brz dot net, don't believe Visual Studio. Simply run your code and it will work. There definitely is such element. It's even stated on MSDN: http://msdn.microsoft.com/en-us/library/ty48b824.aspx
Darin Dimitrov
+1 Great Man! means no need of fiddler.
Brij
Can i specify tracing for particular request?
Brij