In this article I will explain how to post data from one website to another site in ASP.Net using the HttpWebRequest class.
I’ll explain the technique with a small example where I’ll post two parameters First Name and Last Name to the remote website.

Namespaces
You need to import the following namespaces
C#
using System.Text;
using System.Net;
using System.IO;

VB.Net
Imports System.Text
Imports System.Net
Imports System.IO


Posting Data using the HttpWebRequest class
I have added a ASP.Net button on whose click the data is posted to the other remote site.
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string remoteUrl = "http://localhost:54358/PostDataFromOneWebsiteToAnother/Page2_CS.aspx";
    string firstName = "Mudassar";
    string lastName = "Khan";
 
    ASCIIEncoding encoding = new ASCIIEncoding();
    string data = string.Format("FirstName={0}&LastName={1}", firstName, lastName);
    byte[] bytes = encoding.GetBytes(data);
    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(remoteUrl);
    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.ContentLength = bytes.Length;
    using (Stream stream = httpRequest.GetRequestStream())
    {
        stream.Write(bytes, 0, bytes.Length);
        stream.Close();
    }
}

VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim remoteUrl As String = "http://localhost:54358/PostDataFromOneWebsiteToAnother/Page2_VB.aspx"
        Dim firstName As String = "Mudassar"
        Dim lastName As String = "Khan"
        Dim encoding As ASCIIEncoding = New ASCIIEncoding
        Dim data As String = String.Format("FirstName={0}&LastName={1}", firstName, lastName)
        Dim bytes() As Byte = encoding.GetBytes(data)
        Dim httpRequest As HttpWebRequest = CType(WebRequest.Create(remoteUrl), HttpWebRequest)
        httpRequest.Method = "POST"
        httpRequest.ContentType = "application/x-www-form-urlencoded"
        httpRequest.ContentLength = bytes.Length()
        Dim stream As Stream = httpRequest.GetRequestStream
        stream.Write(bytes, 0, bytes.Length)
        stream.Close()
End Sub

Above you will notice that I have a URL of the remote site where I want to post data. I am using the same to create the request. Also you will notice that I have added the parameters as Key-Value pairs that I need to post separated by ampersand (&). These key names will be used in the remote site to fetch the posted values.
Note: Make sure you put the correct PORT Number and URL when running the application for the remote page otherwise data will not get posted.

Fetching the Posted Data
On the Remote Page (here Page2_CS.aspx and Page2_VB.aspx) you need to fetch the posted values in the following way.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form.Count > 0)
    {
        string firstName = Request.Form["FirstName"];
        string lastName = Request.Form["LastName"];
    }
}

VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.Form.Count > 0 Then
            Dim firstName As String = Request.Form("FirstName")
            Dim lastName As String = Request.Form("LastName")
        End If
End Sub


Downloads
With this we come to the end of this article. You can download the complete source code using the download link provided below.

PostDataFromOneWebsiteToAnother.zip