{"id":8982,"date":"2022-09-16T14:59:44","date_gmt":"2022-09-16T09:29:44","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/"},"modified":"2022-09-16T14:59:44","modified_gmt":"2022-09-16T09:29:44","slug":"solved-send-http-post-request-in-net","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/","title":{"rendered":"[Solved] Send HTTP POST request in .NET"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-4015346\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"4015346\" data-parentid=\"4015324\" data-score=\"2576\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>There are several ways to perform HTTP <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Hypertext_Transfer_Protocol#Request_methods\">GET<\/a> and <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Hypertext_Transfer_Protocol#Request_methods\">POST<\/a> requests:<\/p>\n<hr>\n<h2>Method A: HttpClient (Preferred)<\/h2>\n<p>Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+.<\/p>\n<p>It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.nuget.org\/packages\/System.Net.Http\">NuGet package<\/a>.<\/p>\n<pre><code>using System.Net.Http;\n<\/code><\/pre>\n<h3>Setup<\/h3>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/architecture\/antipatterns\/improper-instantiation\/\">It is recommended<\/a> to instantiate one <code>HttpClient<\/code> for your application&#8217;s lifetime and share it unless you have a specific reason not to.<\/p>\n<pre><code>private static readonly HttpClient client = new HttpClient();\n<\/code><\/pre>\n<p>See <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/implement-resilient-applications\/use-httpclientfactory-to-implement-resilient-http-requests#what-is-httpclientfactory\"><code>HttpClientFactory<\/code><\/a> for a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Dependency_injection\">dependency injection<\/a> solution.<\/p>\n<hr>\n<ul>\n<li>\n<p>POST<\/p>\n<pre><code>  var values = new Dictionary&lt;string, string&gt;\n  {\n      { \"thing1\", \"hello\" },\n      { \"thing2\", \"world\" }\n  };\n\n  var content = new FormUrlEncodedContent(values);\n\n  var response = await client.PostAsync(\"http:\/\/www.example.com\/recepticle.aspx\", content);\n\n  var responseString = await response.Content.ReadAsStringAsync();\n<\/code><\/pre>\n<\/li>\n<li>\n<p>GET<\/p>\n<pre><code>  var responseString = await client.GetStringAsync(\"http:\/\/www.example.com\/recepticle.aspx\");\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr>\n<h2>Method B: Third-Party Libraries<\/h2>\n<p><em><strong><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/restsharp\/RestSharp\">RestSharp<\/a><\/strong><\/em><\/p>\n<ul>\n<li>\n<p>POST<\/p>\n<pre><code>   var client = new RestClient(\"http:\/\/example.com\");\n   \/\/ client.Authenticator = new HttpBasicAuthenticator(username, password);\n   var request = new RestRequest(\"resource\/{id}\");\n   request.AddParameter(\"thing1\", \"Hello\");\n   request.AddParameter(\"thing2\", \"world\");\n   request.AddHeader(\"header\", \"value\");\n   request.AddFile(\"file\", path);\n   var response = client.Post(request);\n   var content = response.Content; \/\/ Raw content as string\n   var response2 = client.Post&lt;Person&gt;(request);\n   var name = response2.Data.Name;\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><em><strong><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/flurl.dev\/\">Flurl.Http<\/a><\/strong><\/em><\/p>\n<p>It is a newer library sporting a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Fluent_interface\">fluent API<\/a>, testing helpers, uses HttpClient under the hood, and is portable. It is available via <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.nuget.org\/packages\/Flurl.Http\">NuGet<\/a>.<\/p>\n<pre><code>    using Flurl.Http;\n<\/code><\/pre>\n<hr>\n<ul>\n<li>\n<p>POST<\/p>\n<pre><code>  var responseString = await \"http:\/\/www.example.com\/recepticle.aspx\"\n      .PostUrlEncodedAsync(new { thing1 = \"hello\", thing2 = \"world\" })\n      .ReceiveString();\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>GET<\/code><\/p>\n<pre><code>  var responseString = await \"http:\/\/www.example.com\/recepticle.aspx\"\n      .GetStringAsync();\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr>\n<h2>Method C: HttpWebRequest (not recommended for new work)<\/h2>\n<p>Available in: .NET Framework 1.1+, .NET Standard 2.0+, .NET Core 1.0+. In .NET Core, it is mostly for compatibility &#8212; it wraps <code>HttpClient<\/code>, is less performant, and won&#8217;t get new features.<\/p>\n<pre><code>using System.Net;\nusing System.Text;  \/\/ For class Encoding\nusing System.IO;    \/\/ For StreamReader\n<\/code><\/pre>\n<hr>\n<ul>\n<li>\n<p>POST<\/p>\n<pre><code>  var request = (HttpWebRequest)WebRequest.Create(\"http:\/\/www.example.com\/recepticle.aspx\");\n\n  var postData = \"thing1=\" + Uri.EscapeDataString(\"hello\");\n      postData += \"&amp;thing2=\" + Uri.EscapeDataString(\"world\");\n  var data = Encoding.ASCII.GetBytes(postData);\n\n  request.Method = \"POST\";\n  request.ContentType = \"application\/x-www-form-urlencoded\";\n  request.ContentLength = data.Length;\n\n  using (var stream = request.GetRequestStream())\n  {\n      stream.Write(data, 0, data.Length);\n  }\n\n  var response = (HttpWebResponse)request.GetResponse();\n\n  var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();\n<\/code><\/pre>\n<\/li>\n<li>\n<p>GET<\/p>\n<pre><code>  var request = (HttpWebRequest)WebRequest.Create(\"http:\/\/www.example.com\/recepticle.aspx\");\n\n  var response = (HttpWebResponse)request.GetResponse();\n\n  var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr>\n<h2>Method D: WebClient (Not recommended for new work)<\/h2>\n<p>This is a wrapper around <code>HttpWebRequest<\/code>. Compare with <code>HttpClient<\/code>.<\/p>\n<p>Available in: .NET Framework 1.1+, NET Standard 2.0+, and .NET Core 2.0+.<\/p>\n<p>In some circumstances (.NET Framework 4.5-4.8), if you need to do a HTTP request synchronously, <code>WebClient<\/code> can still be used.<\/p>\n<pre><code>using System.Net;\nusing System.Collections.Specialized;\n<\/code><\/pre>\n<hr>\n<ul>\n<li>\n<p>POST<\/p>\n<pre><code>  using (var client = new WebClient())\n  {\n      var values = new NameValueCollection();\n      values[\"thing1\"] = \"hello\";\n      values[\"thing2\"] = \"world\";\n\n      var response = client.UploadValues(\"http:\/\/www.example.com\/recepticle.aspx\", values);\n\n      var responseString = Encoding.Default.GetString(response);\n  }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>GET<\/p>\n<pre><code>  using (var client = new WebClient())\n  {\n      var responseString = client.DownloadString(\"http:\/\/www.example.com\/recepticle.aspx\");\n  }\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">24<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Send HTTP POST request in .NET <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] There are several ways to perform HTTP GET and POST requests: Method A: HttpClient (Preferred) Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+. It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a &#8230; <a title=\"[Solved] Send HTTP POST request in .NET\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\" aria-label=\"More on [Solved] Send HTTP POST request in .NET\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[352,324,2159,2529,994],"class_list":["post-8982","post","type-post","status-publish","format-standard","hentry","category-solved","tag-net","tag-c","tag-httprequest","tag-httpwebrequest","tag-post"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Send HTTP POST request in .NET - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Send HTTP POST request in .NET - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] There are several ways to perform HTTP GET and POST requests: Method A: HttpClient (Preferred) Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+. It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-16T09:29:44+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Send HTTP POST request in .NET\",\"datePublished\":\"2022-09-16T09:29:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\"},\"wordCount\":216,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\".net\",\"c++\",\"httprequest\",\"httpwebrequest\",\"post\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\",\"name\":\"[Solved] Send HTTP POST request in .NET - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-16T09:29:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Send HTTP POST request in .NET\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Send HTTP POST request in .NET - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Send HTTP POST request in .NET - JassWeb","og_description":"[ad_1] There are several ways to perform HTTP GET and POST requests: Method A: HttpClient (Preferred) Available in: .NET Framework 4.5+, .NET Standard 1.1+, and .NET Core 1.0+. It is currently the preferred approach, and is asynchronous and high performance. Use the built-in version in most cases, but for very old platforms there is a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/","og_site_name":"JassWeb","article_published_time":"2022-09-16T09:29:44+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Send HTTP POST request in .NET","datePublished":"2022-09-16T09:29:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/"},"wordCount":216,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":[".net","c++","httprequest","httpwebrequest","post"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/","url":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/","name":"[Solved] Send HTTP POST request in .NET - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-16T09:29:44+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-send-http-post-request-in-net\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Send HTTP POST request in .NET"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8982"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8982\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}