Software & Finance





ASP.NET - C# HTTP Post Request to webhandler (.ashx) page





 

Here is the C# sample code that makes a HTTP post request to a webhandler (.ashx) file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
   class Program
   {
      static void Main(string[] args)
      {
         string URL = "http://www.softwareandfinance.com/myfirstwebhandler/welcome.ashx";
         WebClient webClient = new WebClient();

         NameValueCollection formData = new NameValueCollection();
         formData["username"] = "testuser";
         formData["password"] = "mypassword";

         byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
         string responsefromserver = Encoding.UTF8.GetString(responseBytes);
         Console.WriteLine(responsefromserver);
         webClient.Dispose();
      }
   }
}