{"id":143,"date":"2011-10-27T13:13:01","date_gmt":"2011-10-27T05:13:01","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=143"},"modified":"2018-09-03T21:10:08","modified_gmt":"2018-09-03T13:10:08","slug":"the-http-request-and-how-it-relates-to-system-net-httplistener","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/the-http-request-and-how-it-relates-to-system-net-httplistener\/","title":{"rendered":"The HTTP request and how it relates to System.Net.HttpListener"},"content":{"rendered":"<p>This is part 2 of the sequel to \"<a href=\"http:\/\/www.techcoil.com\/blog\/?p=136\" target = \"_blank\" title=\"Link to how to build a web based user interaction layer in C#\">How to build a web based user interaction layer in C#<\/a>\".  In this post, I will discuss how we can examine HTTP requests received from clients.<\/p>\n<p>In part 1, we learn <a href=\"http:\/\/www.techcoil.com\/blog\/?p=136\" target = \"_blank\" title=\"Link to how to build a web based user interaction layer in C#\">how we can get an instance of the <code>System.Net.HttpListenerContext<\/code> class whenever a client sends a HTTP request<\/a>.<\/p>\n<h3>Continuing the sequel<\/h3>\n<p>The HTTP request received from the client will be represented as an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerrequest.aspx\" target=\"_blank\" title=\"MSDN reference to the HttpListenerRequest class\"><code>System.Net.HttpListenerRequest<\/code><\/a> class and is available via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenercontext.request.aspx\" target=\"_blank\" title=\"MSDN reference for HttpListenerContext.Request property\"><code>Request<\/code><\/a> property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenercontext.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Net.HttpListenerContext class\"><code>System.Net.HttpListenerContext<\/code><\/a> instance. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nHttpListenerRequest clientRequest = context.Request;\r\n<\/pre>\n<p>From the <code>HttpListenerRequest<\/code> instance, your C# program can examine the HTTP request received from the client.<\/p>\n<h3>The Url requested by the client<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nUri requestUrl = clientRequest.Url;\r\n<\/pre>\n<p>The <code>Url<\/code> property contains an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.uri.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Uri class\">System.Uri<\/a> class that represents the HTTP resource requested by the client. With the <code>Uri<\/code> instance, we can know which resource that a client wants to interact with, via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.uri.absolutepath.aspx\" target=\"_blank\" title=\"MSDN reference for the Uri.AbsolutePath property\"><code>Uri.AbsolutePath<\/code><\/a> property.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring requestAbsolutePath \r\n    = clientRequest.Uri.AbsolutePath;\r\n<\/pre>\n<p>The AbsolutePath property will contain the part of the url that is after the hostname and\/or port and the first <strong>?<\/strong> that denotes the first query string variable. For example, <\/p>\n<p><strong>\/GetPersonName<\/strong> will be returned for<\/p>\n<p><strong>http:\/\/localhost:12345\/GetPersonName?id=1<\/strong> <\/p>\n<p>and <\/p>\n<p><strong>\/GetPersonName&id=1<\/strong> will be returned for<\/p>\n<p><strong>http:\/\/localhost:12345\/GetPersonName&id=1<\/strong>.<\/p>\n<p>If the second case occurs, your C# program can consider the case as a 404 immediately.<\/p>\n<h3>Query strings<\/h3>\n<p>Although we can get the query string via the <code>Url<\/code> property of our <code>HttpListenerRequest<\/code> instance, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerrequest.querystring.aspx\" target=\"_blank\" title=\"MSDN reference for HttpListenerRequest.QueryString property\"><code>QueryString<\/code><\/a> property of the <code>HttpListenerRequest<\/code> instance provides better programmatic access to the query string values submitted by the client. The <code>QueryString<\/code> property is an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.namevaluecollection.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Collections.Specialized.NameValueCollection class\"><code>System.Collections.Specialized.NameValueCollection<\/code><\/a> class. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nNameValueCollection queryStringCollection \r\n    = clientRequest.QueryString;\r\n\/\/ Get the value of the variable named as &quot;id&quot;\r\n\/\/ id will be null when client does not send \r\n\/\/ any query string variable named as &quot;id&quot;\r\nstring id = queryStringCollection&#x5B;&quot;id&quot;];\r\n<\/pre>\n<h3>The HTTP method<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring requestHttpMethod = clientRequest.HttpMethod;\r\n<\/pre>\n<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.webrequestmethods.http.aspx\" target=\"_blank\" title=\"MSDN reference for System.Net.WebRequestMethods.Http\"><code>System.Net.WebRequestMethods.Http<\/code><\/a> class supplies the possible values that may be contained in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerrequest.httpmethod.aspx\" target=\"_blank\" title=\"MSDN reference for HttpListenerRequest.HttpMethod\"><code>HttpMethod<\/code><\/a> property of the <code>HttpListenerRequest<\/code> instance.  <\/p>\n<h3>The content type<\/h3>\n<p>The content type denotes the type of content that is contained in the HTTP request. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstring requestContentType = clientRequest.ContentType;\r\n<\/pre>\n<p>This value is used in conjunction with the HTTP method. <\/p>\n<p>For HTTP Get, the browser typically don't fill up the content type field. <\/p>\n<p>For HTTP post, two possible values are: <\/p>\n<ul>\n<li><strong>application\/x-www-form-urlencoded<\/strong><\/li>\n<li><strong>multipart\/form-data; boundary=----SomeRandomText<\/strong><\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.techcoil.com\/blog\/2011\/10\/15\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" target=\"_blank\" title=\"Downloading a file via HTTP post and HTTP get in C# \">Example for client sending a HTTP Post request with content type <strong>application\/x-www-form-urlencoded<\/strong><\/a>.<\/p>\n<p><a href=\"http:\/\/www.techcoil.com\/blog\/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c\/\" target=\"_blank\" title=\"Upload large http multipart request with System.Net.HttpWebRequest in C#\">Example for client sending a HTTP Post request with content type <strong>multipart\/form-data; boundary=----SomeRandomText<\/strong><\/a>.<\/p>\n<h3>The HTTP request body<\/h3>\n<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerrequest.inputstream.aspx\"><code>InputStream<\/code><\/a> property of the <code>HttpListenerRequest<\/code> instance.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nStream requestBodyStream = clientRequest.InputStream;\r\n<\/pre>\n<h3>Next in the line<\/h3>\n<p>This concludes part 2 of \"<a href=\"http:\/\/www.techcoil.com\/blog\/?p=136\" target = \"_blank\" title=\"Link to how to build a web based user interaction layer in C#\">How to build a web based user interaction layer in C#<\/a>\". <\/p>\n<p>In part 3, I will discuss <a href=\"http:\/\/www.techcoil.com\/blog\/?p=145\" title=\"Link to Http response and how it relates to System.Net.HttpListener\">the HTTP response and how it relates to <code>System.Net.HttpListener<\/code><\/a>. <\/p>\n<h3>Related posts<\/h3>\n<p>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. \ud83d\ude42 For the adventurous, you can probably use the methods discussed in these post<\/p>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" target=\"_blank\">Downloading a file via HTTP post and HTTP get in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" target=\"_blank\">Sending a file and some form data via HTTP post in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c\/\" target=\"_blank\">Uploading large HTTP multipart request with <code>System.Net.HttpWebRequest<\/code> in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/handling-web-server-communication-feedback-with-system-net-webexception\/\" target=\"_blank\">Handling web server communication feedback with <code>System.Net.WebException<\/code> in C#<\/a><\/li>\n<\/ul>\n<h3>Further reading elsewhere<\/h3>\n<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerrequest.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Net.HttpListenerRequest class\">MSDN reference for the <code>System.Net.HttpListenerRequest<\/code> class<\/a>. <\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-2j\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-2j&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-2j&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F143&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>This is part 2 of the sequel to &#8220;<a href=\"http:\/\/www.techcoil.com\/blog\/?p=136\" target = \"_blank\" title=\"Link to how to build a web based user interaction layer in C#\">How to build a web based user interaction layer in C#<\/a>&#8220;.  In this post, I will discuss how we can examine HTTP requests received from clients.<\/p>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,23,59,62,58,61],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-2j","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}