I'd like to create my own custom HTTP requests. The WebClient class is very cool, but it creates the HTTP requests automatically. I'm thinking I need to create a network connection to the web server and pass my data over that stream, but I'm unfamiliar with the library classes that would support that kind of thing.
(Context, I'm working on some code for a web programming class that I'm teaching. I want my students to understand the basics of what's happening inside the "black box" of HTTP.)
Check out
System.Net.Sockets.TcpClient
if you want to write your own low level client. For HTTP GET's and POST's you can use theHttpWebRequest
andHttpWebResponse
classes though.If you are really masochistic you could go lower than
TcpClient
and implement your ownSocket
, see the Socket class.To really understand the internals of the HTTP protocol you could use TcpClient class:
Another possibility is to activate tracing by putting the following into your
app.config
and just use WebClient to perform an HTTP request:Then you can perform an HTTP call:
And finally analyze the network traffic in the generated
network.log
file.WebClient
will also follow HTTP redirects.Use the WebRequest or WebResponse classes, as required.
If you need to go lower level than these provide, look at the other System.Net.Sockets.*Client classes such as TcpClient.
Use WFetch for the demonstration.
As for programming, HttpWebRequest lets you control quite a bit about the request - again if it's for a demonstration I'd use Wireshark to sniff what's going over the wire when you do various tasks with the HttpWebRequest
In my response to this SO post SharePoint 2007, how to check if a folder exists in a document library I have included the basics of creating HttpWebRequest and reading response.
Edit: added a code example from the post above