Publish Article
Go Freelance

HttpWebRequest Post Request To Another Web Site With Redirection


If you are seeing this section and do not see download links, this means that you are not logged into our site. If you already are a member, click on the login link and login into site and come back to this page for downloading the control files. If you are not a member, click on registration link to become a Winista member and download the control for free.

In my earlier article How to use HttpWebRequest to send POST request to another web server? I explained how you can use utilize System.Net.HttpWebRequest class to send data to another web server. And example I gave was of PayPal payment. This solves the problem that you are able to send the data. But there is another set of problem that it does not solve and that is that you want the user to redirected to that web site along with the data.

In ASP.Net 1.1 and earlier, framrwork did not allow you to have more than one FORM tags in the page. So you had to play the trick of displaying only one FORM tag at a time based on some run time conditions. Now ASP.Net 2.0 has introduced using multiple targets for button or similar control clicks. But this has its drawbacks too. For example if you have more than one FORM tags on your page and you use the new mechanism of choosing target page from button click, then your target web server will be getting muliple sets of data for POST request. For GET request you can embed all the data in Query String. But for POST you are kind of ourt of luck.

This article will dexcribe a technique that makes use of client side javascript along with server side event handling to send data to PayPal site and sending user to that site with it to complete the transaction. Lets say that you gather order data from user for Paypal transaction and user is clicking on a button on your page to pay. This technique involves very simple steps.

  • Add a Lieral control on your page. This where we will render the form that we want to send to Paypal site. Add one more Literal control at the bottom of the page where we will render client side javascript to perform POST action.
  • In OnClick event handler of the button, create the FORM tag with all the data that you want to POST to PayPal and then set this FORM tag text into the lteral control.
  • Create client side javascript that will get the FORM element and call submit method on it.

Following code snippet shows all the steps in action. You can download the project with this article to try it yourself.

protected void OnBuy(object sender, EventArgs e)
    ctlPaypalForm.Text = _GetPaypalForm("My Item", "25.99");
    String strJS = _GetPayPalPostJS("_xclick");
    ctlPostScript.Text = strJS;
private String _GetPayPalPostJS(String strFormId)
    StringBuilder strScript = new StringBuilder();
    strScript.Append("<script language='javascript'>");
    strScript.Append("var ctlForm = document.forms.namedItem('{0}');");
    return String.Format(strScript.ToString(), strFormId);
private String _GetPaypalForm(String strItemName, String strPrice)
    StringBuilder strForm = new StringBuilder();
    strForm.Append("<form id=\"_xclick\" name=\"_xclick\" target=\"_blank\" 
        action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">");
    strForm.Append("<input type=\"hidden\" name=\"cmd\" value=\"_cart\">");
    strForm.Append("<input type=\"hidden\" name=\"business\" value=\"me@mybiz.com\">");
    strForm.Append("<input type=\"hidden\" name=\"item_name\" value=\"{0}\">");
    strForm.Append("<input type=\"hidden\" name=\"amount\" value=\"{1}\">");
    strForm.Append("<input type=\"image\" src=\"http://www.paypal.com/images/sc-but-01.gif\" 
        border=\"0\" name=\"submit\" alt=\"Make payments with PayPal!\">");
    strForm.Append("<input type=\"hidden\" name=\"add\" value=\"1\">");
    return String.Format(strForm.ToString(), strItemName, strPrice);
Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix