Advertisements

The HTTP request and how it relates to System.Net.HttpListener

This is part 2 of the sequel to "How to build a web based user interaction layer in C#". In this post, I will discuss how we can examine HTTP requests received from clients.

In part 1, we learn how we can get an instance of the System.Net.HttpListenerContext class whenever a client sends a HTTP request.

Continuing the sequel

The HTTP request received from the client will be represented as an instance of the System.Net.HttpListenerRequest class and is available via the Request property of the System.Net.HttpListenerContext instance.

HttpListenerRequest clientRequest = context.Request;

From the HttpListenerRequest instance, your C# program can examine the HTTP request received from the client.

The Url requested by the client

Uri requestUrl = clientRequest.Url;

The Url property contains an instance of the System.Uri class that represents the HTTP resource requested by the client. With the Uri instance, we can know which resource that a client wants to interact with, via the Uri.AbsolutePath property.

string requestAbsolutePath 
    = clientRequest.Uri.AbsolutePath;

The AbsolutePath property will contain the part of the url that is after the hostname and/or port and the first ? that denotes the first query string variable. For example,

/GetPersonName will be returned for

http://localhost:12345/GetPersonName?id=1

and

/GetPersonName&id=1 will be returned for

http://localhost:12345/GetPersonName&id=1.

If the second case occurs, your C# program can consider the case as a 404 immediately.

Query strings

Although we can get the query string via the Url property of our HttpListenerRequest instance, the QueryString property of the HttpListenerRequest instance provides better programmatic access to the query string values submitted by the client. The QueryString property is an instance of the System.Collections.Specialized.NameValueCollection class.

NameValueCollection queryStringCollection 
    = clientRequest.QueryString;
// Get the value of the variable named as "id"
// id will be null when client does not send 
// any query string variable named as "id"
string id = queryStringCollection["id"];

The HTTP method

string requestHttpMethod = clientRequest.HttpMethod;

With this information, your C# program can decide how to process input information from the client. For instance, if the client had indicated a HTTP Get, your C# program can take it that if there are any input from the client, it will be supplied as query strings. The System.Net.WebRequestMethods.Http class supplies the possible values that may be contained in the HttpMethod property of the HttpListenerRequest instance.

The content type

The content type denotes the type of content that is contained in the HTTP request.

string requestContentType = clientRequest.ContentType;

This value is used in conjunction with the HTTP method.

For HTTP Get, the browser typically don't fill up the content type field.

For HTTP post, two possible values are:

  • application/x-www-form-urlencoded
  • multipart/form-data; boundary=----SomeRandomText

Example for client sending a HTTP Post request with content type application/x-www-form-urlencoded.

Example for client sending a HTTP Post request with content type multipart/form-data; boundary=----SomeRandomText.

The HTTP request body

The HTTP request body is most probably filled up by the client for a HTTP post. To read the HTTP request body in your C# program, you can use the InputStream property of the HttpListenerRequest instance.

Stream requestBodyStream = clientRequest.InputStream;

Next in the line

This concludes part 2 of "How to build a web based user interaction layer in C#".

In part 3, I will discuss the HTTP response and how it relates to System.Net.HttpListener.

Related posts

To digress, the following is a list of posts that relates to sending HTTP requests to web servers. Feel free to look through them as well. 🙂 For the adventurous, you can probably use the methods discussed in these post

Further reading elsewhere

What I have mentioned in this post is probably enough for your C# program to process HTTP requests received from clients in most situations. If your program needs to get more information from HTTP requests received from clients, check out the MSDN reference for the System.Net.HttpListenerRequest class.

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people.

Advertisements