title: How to submit form data to a external URL using C# in my .NET Core 5 application?
body: <p>I need to send form data to an external URI and get the response which is a page redirect.  I've written the code to do this but the remote server isn't accepting it, the reasons are not specified.  If I create the form in a HTML page and submit it from there, it works perfectly.</p>
  <p>Here is the form I need to submit:</p>
  <pre><code>&lt;form action=&quot;https://someurl.com/make&quot; method=POST&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;test&quot; value=&quot;42&quot;&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;version&quot; value=&quot;123&quot;&gt;
      &lt;input type=submit value=&quot;Send&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  <p>So, to send this form data to <code>someurl.com</code> instead of the user having to do it, I tried to use a <code>WebClient</code> because I'm posting to an external URL for this, I also use string interpolation for values that are passed when this is called.</p>
  <pre><code>public async Task&lt;string&gt; Send_Version(string versionNumber = &quot;0&quot;)
  {
      try
      {
          string html = $&quot;test=42&amp;version={versionNumber}&quot;;
  
          using (WebClient webclient = new())
          {
              webclient.Headers[HttpRequestHeader.ContentType] = &quot;application/x-www-form-urlencoded&quot;;
              string pagesource = webclient.UploadString(&quot;www.someurl.com/make&quot;, html);
              
              return pagesource.ToString();
              }                
          }
          catch (Exception ex)
          {
              errorString = $&quot;Error&quot;;
              return errorString;
          }            
      }
  </code></pre>
  <p>As you can see I use the <code>WebClient</code> so that is disposes correctly, I add a <code>Content-Type</code> and then I <code>UploadString</code> via the web client with both the URL and my data <code>html</code>.</p>
  <p>The remote server doesn't like this way of doing it but my question is, does this look accurate for posting form data to a remote url with c# or have I missed something critical?</p>