![]() |
General Programming »
Internet / Network »
HTTP
Intermediate
HTTPeep - an HTTP inspectorBy Ashley van GervenHandy C# utility to perform custom HTTP requests and view the raw HTTP response. |
C#.NET 1.1, Win2K, WinXPVS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This tool allows you to inspect (or "peep at") the RAW HTTP response of a URL. You can build the request using the settings form (including POST variables, user agent etc.). I initially made this tool a couple of years ago, and it continued to be extremely useful, so I improved it over time, and resolved the main bugs. This app is a work in progress, but if I wait till I'm "finished", this code will never get posted - you know how it goes with side-projects. I hope that in the mean time people will find this useful as is.
The user interface is fairly self-explanatory - specify the URL ("http://" is optional), and optionally the Request Settings. Available settings are:
Once the response has been fully downloaded, HTTPeep will show you:
MaxLength property of the TextBox (currently set to 100,000 characters). So you will need to right-click and save to a file to view the content. If the response content-type is non-text, it also won't display here, and again you can right-click to save the content as a file.

By default, the Raw checkbox is unchecked and you can add POST variables one by one. To edit an existing item, just double click it. To remove an item, select it and hit Delete key. You can save or load the POST variables to a text file. The format of the text file is intentionally simple so that you can easily create one using a text editor:
[User]
one
[Pass]
two
[Key]
value value
value
value
(Note: I haven't provided for an escape of "[" - so will be a problem if your POST variable starts with this.)
From a technical point of view, there is nothing fancy under the hood. I am simply configuring the properties of a System.Net.HttpWebRequest object, and calling the GetResponse() method to return a HttpWebResponse object. Note that a CookieContainer is set, and cookies are retained while the app remains open.
The response content is saved to a unique temporary file:
TmpFilename = Path.GetTempPath() + "http_content" + requestStart.Ticks + ".tmp";
The code to download the content stream from the HttpWebResponse object into a file looks like this:
Stream rcvStream = resp.GetResponseStream();
byte[] respBytes = new byte[256];
int byteCount;
FileStream fs = new FileStream(TmpFilename, FileMode.Create, FileAccess.Write);
do
{
byteCount = rcvStream.Read(respBytes, 0, 256);
fs.Write(respBytes, 0, byteCount);
} while (byteCount > 0);
The appSettings keys in the .config file are self-explanatory. All but the first affect the Settings form:
DefaultURL (for the main form)
DefaultUserAgent
DefaultAccept
DefaultReferer
DefaultCustomHeaders
UserAgentMenu - pipe-delimited list of values (name|string|name|string...). The current UA strings are IE 6, Firefox 1.0, Nokia 6230:

Even though Ctrl-C, Ctrl-V, Ctrl-X work by to copy, paste, cut on the standard .NET TextBox control, Ctrl-A does not select all as you might expect. Instead you get an annoying beep.
So you'd expect a simple solution would work - i.e., code a KeyDown or KeyUp event handler and set e.Handled to true so Windows knows you've handled the key-combination. Hmmm... that doesn't work. Guess what? Only setting e.Handled in the KeyPress event handler prevents this beep. Unfortunately, KeyPressEventArgs doesn't give you all the additional info that KeyEventArgs does so forget putting your code in the KeyPress event handler. But I wouldn't bother writing all this if I didn't have a solution: just set a flag in the KeyDown event handler and set e.Handled in your KeyPress event handler (which is called after KeyDown) if the flag is set! Code as follows:
private void TxtUrl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
keyHandled = false;
if (e.KeyValue == 13)
{
BtnGo.PerformClick();
keyHandled = true;
}
else if (e.Control && e.KeyCode == Keys.A)
{
((TextBox)sender).SelectAll();
((TextBox)sender).Focus();
keyHandled = true;
}
}
private void TxtUrl_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (keyHandled)
e.Handled = true;
}
To get an XML response from a webservice, you can use HTTPeep to POST some XML to your .asmx URL:
I have no idea when I'll get around to doing any of these:
CookieContainer object.
Feel free to make changes to suit your own requirements.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Mar 2005 Editor: Nishant Sivakumar |
Copyright 2005 by Ashley van Gerven Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |