Demo it now

Oftentimes, such as when hacking demos, we want our client side app talking to some form backend without spending too much time on it. One way to do it in a Microsoft environment is to do a quick POST request using HTTPWebRequest object. HTTPWebRequest does exactly what the name suggests and saves us a lot of time from having to write our own HTTP calls.

As simple as URL

During the first step, we would split our URL such as into the address of the server and the body containing our POST variables. Of course, if we were creating a GET request, then we would not need to do that.

String url = "";
string [] httpSend = url.Split('?');

Uri uri = new Uri(httpSend[0]);

Next, we would create HTTPWebRequest object using the above uri in the constructor and assign whichever HTTP header variables we need.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.AllowAutoRedirect = true;
request.KeepAlive = true;
request.UserAgent = "Past5 demo app";
request.Accept = "*/*";

Since we are POSTing to some form backend, we would also need to tell the server that we are submitting a form:

request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

And finally, since we are sending a POST request, our URL GET variables will go into the body of our HTTP request.

byte[] content = Encoding.UTF8.GetBytes(httpSend[1]);
request.ContentLength = content.Length;
using (Stream stream = request.GetRequestStream()) {
	stream.Write(content, 0, content.Length);

The whole HTTP request

User-Agent: Past5 demo app
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 23
Expect: 100-continue
Connection: Keep-Alive


P.S. And what about the response?

To obtain the response from the server, we would simply use HTTPWebResponse object and use StreamReader to get the body of the response.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
if (request.HaveResponse) String responseBody = reader.ReadToEnd();

blog comments powered by Disqus