tags:

views:

32

answers:

2

I'm trying to hunt down why a POST request from a C# script isn't working, when the same request works fine in Python. I want to be able to have all the data sent by the script, and the response from the sever, to be displayed on the screen so that I can work out what the difference is between what the C# and Python scripts are sending.

In Python I can do this with the standard httplib2 library by just using:

httplib2.debuglevel = 1

This produces the following output (as an example):

reply: 'HTTP/1.1 201 Created\r\n'
header: Date: Tue, 05 Oct 2010 09:25:42 GMT
header: Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny9 with Suhosin-Patch
header: X-Powered-By: PHP/5.2.6-1+lenny9
header: Location: http://example.org/api/2
header: Content-Length: 0
header: Content-Type: text/html
send: 'GET /api/2 HTTP/1.1\r\nHost: example.org\r\naccept-encoding: gzip, deflate\r\nuser-agent: Python-httplib2/$Rev$\r\n\r\n'

Is there a way to produce similar output in C# using the HttpWebRequest class?

I've seen mention of Fiddler in another question, but I'm running Linux and Fiddler appears to be for Windows only.

A: 

Yes you can and I have used it successfully before. If you are using C# I imagine there is a config file that you acn turn System.Net tracing on. I am not sure if Mono has this - if you are using Mono.

Have a look here: http://msdn.microsoft.com/en-us/library/ty48b824.aspx

Aliostad
I've tried turning tracing on, you can do it in Mono by using #define TRACE in the source file and running the executable with --trace. The only problem is that this shows me every single function call, whereas I really only want to see the HTTP request/response (in plain text, as if I'd constructed them manually).
You need the "Verbose" in there. Not sure about how to do in Mono.
Aliostad
+1  A: 

You can spin through resp.Headers.AllKeys and then dump the key and its value, though there is sometimes a degree of translation going on (most obviously when it is doing auto-redirect-following).

You can use System.Net tracing.

You can use ethereal which has a linux version. I don't use it for such things these days as Fiddler is indeed handier for such cases, but I used to use ethereal in the past, and sometimes seeing what is actually on the wire rather than what the code is saying is the best way to go (as you aren't depending on possibly buggy code to tell you if the code is buggy).

Jon Hanna