Pages

Saturday 26 January 2013

How to transfer data from one website to another in ASP.Net

Abstract: Here Mudassar Ahmed Khan has explained how to post data in form to another website in another domain using ASP.Net using HttpWebRequest class.
You can also refer to it as
1. How to send data from one website to another in ASP.Net
2. How to pass data from one website to another in ASP.Net


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

No comments:

Post a Comment