{"id":136,"date":"2011-10-27T13:13:05","date_gmt":"2011-10-27T05:13:05","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=136"},"modified":"2018-09-03T21:10:39","modified_gmt":"2018-09-03T13:10:39","slug":"how-to-build-a-web-based-user-interaction-layer-in-c","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-build-a-web-based-user-interaction-layer-in-c\/","title":{"rendered":"How to build a web based user interaction layer in C#"},"content":{"rendered":"<p>With the ubiquity of web browsers, it can be ideal for the user interaction layer of applications to be web based. The most common approaches to building web based applications is to write server side scripts running on web servers. However, these approaches require server programs to be present in the production environment.<\/p>\n<p>What if you want the web server functionality to be contained in your C# program? In C#, there is a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistener.aspx\" target=\"_blank\" title=\"MSDN reference to the System.Net.HttpListener class\"><code>System.Net.HttpListener<\/code><\/a> class which listens for HTTP requests from clients.<\/p>\n<p>This post is part 1 of the sequel. In this post, I will introduce the <code>HttpListener<\/code> class and how we can use it to receive HTTP requests from clients in our C# program.<\/p>\n<h3>Accept incoming HTTP request with HttpListener<\/h3>\n<p>To enable your C# program to have a web based user interaction layer, you will need to specify at least one url for HTTP clients to connect to your C# program.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Create an instance of the httpListener\r\nHttpListener httpListener = new HttpListener();\r\n\/\/ Add a prefix\/url that the httpListener will listen to\r\nhttpListener.Prefixes.Add(&quot;http:\/\/*:12345\/&quot;);\r\n\/\/ Start the http listener\r\nhttpListener.Start();\r\n<\/pre>\n<p>That will get your <code>HttpListener<\/code> ready in receiving HTTP requests from <strong>clients with any hostname<\/strong>. If you want to limit access by the hostname, replace * with the hostname that your <code>HttpListener<\/code> allow. For instance, if you only want clients to access your <code>HttpListener<\/code> via localhost, use the following code instead:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Create an instance of the httpListener\r\nHttpListener httpListener = new HttpListener();\r\n\/\/ Add a prefix\/url that the httpListener will listen to\r\nhttpListener.Prefixes.Add(&quot;http:\/\/localhost:12345\/&quot;);\r\n\/\/ Start the http listener\r\nhttpListener.Start();\r\n<\/pre>\n<p>When a HTTP client sends a HTTP request to your <code>HttpListener<\/code>, an instance of the <a title=\"MSDN reference for the System.Net.HttpListenerContext class\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenercontext.aspx\" target=\"_blank\"><code>System.Net.HttpListenerContext<\/code><\/a> class will be created. We can use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistener.getcontext.aspx\" target=\"_blank\"><code>GetContext<\/code><\/a> method of the <code>HttpListener<\/code> to get the <code>HttpListenerContext<\/code> instance whenever a client sends a HTTP request to one of the prefixes that our <code>HttpListener<\/code> listens to.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Get the HttpListenerContext when there is \r\n\/\/ an available HTTP request from the client.  \r\n\/\/ This code will block until a HTTP request \r\n\/\/ is received via one of the prefixes.\r\nHttpListenerContext context = httpListener.GetContext();\r\n<\/pre>\n<p>With the <code>HttpListenerContext<\/code> instance, your C# program can analyse the HTTP request from the client and create a HTTP response to send back to the client.<\/p>\n<h3>Next in the line<\/h3>\n<p>This concludes my discussion on giving your C# program the capability to accept HTTP request from clients. Where to go next? <\/p>\n<p>Part 2 discusses <a href=\"http:\/\/www.techcoil.com\/blog\/?p=143\" title=\"Link to The HTTP request and how it relates to System.Net.HttpListener\"> how we can process the HTTP request using the facilities provided by the <code>System.Net.HttpListenerRequest<\/code> class<\/a>.<\/p>\n<p>Part 3 discusses <a href=\"http:\/\/www.techcoil.com\/blog\/?p=145\" title=\"Link to The HTTP response and how it relates to System.Net.HttpListener\">how we can prepare a HTTP response using the facilities provided by the <code>System.Net.HttpListenerResponse<\/code> class to send back to the client<\/a>.<\/p>\n<p>Part 4 concludes the sequel by discussing <a href=\"http:\/\/www.techcoil.com\/blog\/?p=146\" title=\"Link to A mechanism to serve HTTP requests in C#\">how to implement a simple mechanism to interact with the user<\/a> with the concepts discussed in the first 3 parts.<\/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<\/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 System.Net.HttpWebRequest 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 System.Net.WebException in C#<\/a><\/li>\n<\/ul>\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-2c\" 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-2c&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-2c&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%2F136&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>With the ubiquity of web browsers, it can be ideal for the user interaction layer of applications to be web based. The most common approaches to building web based applications is to write server side scripts running on web servers. However, these approaches require server programs to be present in the production environment.<\/p>\n<p>What if you want the web server functionality to be contained in your C# program? In C#, there is a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistener.aspx\" target=\"_blank\" title=\"MSDN reference to the System.Net.HttpListener class\"><code>System.Net.HttpListener<\/code><\/a> class which listens for HTTP requests from clients.<\/p>\n<p>This post is part 1 of the sequel. In this post, I will introduce the <code>HttpListener<\/code> class and how we can use it to receive HTTP requests from clients in our C# program.<\/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,56,63,55,57],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-2c","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/136"}],"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=136"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/136\/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=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}