{"id":145,"date":"2011-10-27T13:12:56","date_gmt":"2011-10-27T05:12:56","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=145"},"modified":"2018-09-03T21:09:46","modified_gmt":"2018-09-03T13:09:46","slug":"the-http-response-and-how-it-relates-to-system-net-httplistener","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/the-http-response-and-how-it-relates-to-system-net-httplistener\/","title":{"rendered":"The HTTP response and how it relates to System.Net.HttpListener"},"content":{"rendered":"<p>This is part 3 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 to send a HTTP response back to the client.<\/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 System.Net.HttpListenerContext class whenever a client sends a HTTP request<\/a>.<\/p>\n<p>In part 2, we learn <a href=\"http:\/\/www.techcoil.com\/blog\/?p=143\" target=\"_blank\" title=\"Link to The HTTP request and how it relates to System.Net.HttpListener\">how we can process the HTTP request received from the client in your C# program<\/a>. <\/p>\n<h3>Continuing the sequel<\/h3>\n<p>An instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Net.HttpListenerResponse class\">System.Net.HttpListenerResponse<\/a> class will be available via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenercontext.response.aspx\" target=\"_blank\" title=\"MSDN reference to HttpListenerContext.Response property\">Response<\/a> property from the same <code>HttpListenerContext<\/code> instance that was received from the <code>HttpListener<\/code> instance when a client sends a HTTP request. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nHttpListenerResponse serverResponse = context.Request;\r\n<\/pre>\n<p>We can use the <code>HttpListenerResponse<\/code> instance to send build a HTTP response to send back to the client.<\/p>\n<h3>The HTTP status code<\/h3>\n<p>The <a href=\"http:\/\/www.techcoil.com\/blog\/deciding-which-http-status-code-to-use-in-a-http-response\/\" title=\"Deciding which HTTP status code to use in a HTTP response\" target=\"_blank\">HTTP status code<\/a> is a way to reflect the outcome of processing the HTTP request that the client sent. For instance, to indicate that a HTTP request is successfully processed, we set 200 to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.statuscode.aspx\" target=\"_blank\" title=\"MSDN reference to System.Net.HttpListenerRequest.StatusCode property\"><code>StatusCode<\/code><\/a> property of the <code>HttpListenerResponse<\/code> instance. We can also use the <code>OK<\/code> value from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httpstatuscode.aspx\" target=\"_blank\" title=\"MSDN reference to the System.Net.HttpStatusCode enumeration\">System.Net.HttpStatusCode<\/a> enumeration to do that.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nserverResponse.StatusCode = (int) System.Net.HttpStatusCode.OK;\r\n<\/pre>\n<h3>The content type<\/h3>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.contenttype.aspx\" target=\"_blank\" title=\"MSDN reference to HttpListenerResponse.ContentType property\">ContentType<\/a> property of the <code>HttpListenerResponse<\/code> class can be set to let the client know what kind of content is contained in the HTTP response body. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Indicate that the response body contain a HTML document\r\nserverResponse.ContentType = &quot;text\/html&quot;;\r\n<\/pre>\n<h3>The length of the content<\/h3>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.contentlength64.aspx\" target=\"_blank\" title=\"MSDN reference to HttpListenerResponse.ContentLength64 property\"><code>ContentLength64<\/code><\/a> property of the <code>HttpListenerResponse<\/code> class describe the size of the HTTP response body that the client will be receiving, in bytes.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Set the size of the HTML file as the content length\r\n\/\/ of the HTTP response\r\nserverResponse.ContentLength64 = htmlFileInfo.Length;\r\n<\/pre>\n<h3>The content<\/h3>\n<p>With the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.outputstream.aspx\" target=\"_blank\" title=\"MSDN reference to HttpListenerResponse.OutputStream\"><code>OutputStream<\/code><\/a> property of the HttpListenerResponse instance, you can fill the contents for the HTTP response body. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nSystem.IO.Stream serverResponseOutput = serverResponse.OutputStream;\r\n<\/pre>\n<h3>Send the response back to the client<\/h3>\n<p>After you are done with filling up the <code>HttpListenerResponse<\/code> instance with information for the HTTP response, you need to call the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/9z7fhd98.aspx\" target=\"_blank\" title=\"MSDN reference for HttpListenerResponse.Close method\"><code>Close<\/code><\/a> method to send it back to the client.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nserverResponse.Close();\r\n<\/pre>\n<h3>Next in the line<\/h3>\n<p>This concludes part 3 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 4, I will sum up the sequel with <a href=\"http:\/\/www.techcoil.com\/blog\/?p=146\" target=\"_blank\">how to implement a mechanism for serving HTTP requests in C#<\/a>.<\/p>\n<h3>Further reading elsewhere<\/h3>\n<p>What I had mentioned in this post is probably enough for your C# program to construct HTTP responses back to clients in most situation. If your program needs to furnish more information in the HTTP responses to clients, check out the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httplistenerresponse.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Net.HttpListenerResponse class\">MSDN reference for the <code>System.Net.HttpListenerResponse<\/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-2l\" 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-2l&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-2l&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%2F145&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 3 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 to send a HTTP response back to the client.<\/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,29,60],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-2l","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/145"}],"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=145"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/145\/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=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}