Click here to Skip to main content
Licence CPOL
First Posted 5 Nov 2007
Views 7,267
Downloads 42
Bookmarked 13 times

Posting Data from Web Server A to Web Server B

By | 5 Nov 2007 | Article
You can post data using the POST method of a form attribute. The data can be sent and recieved over different web servers.

Introduction

When you want to transfer data from one web server to another web server through an ASP.NET application, then you can use the POST method to do the same.

The POST method is used to request that the origin server accepts the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Background

Responses to this method (POST method) are not cacheable, unless the response includes the appropriate Cache-Control or Expires header fields. However, the 303 response can be used to direct the user agent to retrieve a cacheable resource.

Using the code

Just call PostMe method and add the input parameters to it:

/// <summary>
/// Post Method to Send credentials
/// </summary>
/// <param name="LoginName"></param>
/// <param name="Password"></param>
/// <param name="isChecked"></param>
void PostMe(string LoginName, string Password, string isChecked)
{
    RemotePost myremotepost = new RemotePost();
    myremotepost.Url = "Default2.aspx";
     /* you can also specify other web server url here.
      * //System.Configuration.ConfigurationManager.AppSettings[
      *           "OtherWebServerURL"].ToString() + "/Default.aspx"; 
      */
    myremotepost.Add("field1", LoginName);
    myremotepost.Add("field2", Password);
    myremotepost.Add("field3", isChecked.ToString());
    myremotepost.Post();
    Session["logoutStatus"] = null;
}

/// <summary>
/// Public Class to Post the Credentials to StoryApp
/// </summary>
public class RemotePost
{
    private System.Collections.Specialized.NameValueCollection Inputs = 
            new System.Collections.Specialized.NameValueCollection();

    public string Url = "";
    public string Method = "post";
    public string FormName = "form1";

    public void Add(string name, string value)
    {
        Inputs.Add(name, value);
    }

    public void Post()
    {
        System.Web.HttpContext.Current.Response.Clear();

        System.Web.HttpContext.Current.Response.Write("<html><head>");

        System.Web.HttpContext.Current.Response.Write(string.Format(
               "</head><body onload=\"document.{0}.submit()\">", FormName));
        System.Web.HttpContext.Current.Response.Write(string.Format(
               "<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >", 
               FormName, Method, Url));
        for (int i = 0; i < Inputs.Keys.Count; i++)
        {
            System.Web.HttpContext.Current.Response.Write(string.Format(
              "<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", 
              Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
        }
        System.Web.HttpContext.Current.Response.Write("</form>");
        System.Web.HttpContext.Current.Response.Write("</body></html>");

        System.Web.HttpContext.Current.Response.End();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

San02



India India

Member

~Sanjeev

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 6 Nov 2007
Article Copyright 2007 by San02
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid