The following CSharp code snippet shows how to submit an HttpWebRequest with POST data. The POST parameters for the HttpWebRequest are passed as a Dictionary object to the method, which returns the result page of the request.

[...]

using System.Net;
using System.IO;
using System.Web;  // also add a reference to System.web.dll for HttpUtility class to be found

[...]


private string HttpPostRequest(string url, Dictionary<string,string> postParameters)
{
    string postData = "";

    foreach (string key in postParameters.Keys)
    {
        postData += HttpUtility.UrlEncode(key) + "="
              + HttpUtility.UrlEncode(postParameters[key]) + "&";
    }

    HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    myHttpWebRequest.Method = "POST";

    byte[] data = Encoding.ASCII.GetBytes(postData);

    myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
    myHttpWebRequest.ContentLength = data.Length;

    Stream requestStream = myHttpWebRequest.GetRequestStream();
    requestStream.Write(data, 0, data.Length);
    requestStream.Close();

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

    Stream responseStream = myHttpWebResponse.GetResponseStream();

    StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);

    string pageContent = myStreamReader.ReadToEnd();

    myStreamReader.Close();
    responseStream.Close();

    myHttpWebResponse.Close();

    return pageContent;
}
This website is making use of cookies for website analysis. Data is collected anonymously and solely for the purpose of improving the website. Do you agree to the use of cross-session cookies?