Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Posting to remote web page with 1.1

I am trying to post some form data from a 1.1 aspx page to a remote web site.
I have been using this as a guideline
http://www.c-sharpcorner.com/UploadFile/desaijm/ASP.NetPostURL11282005005516AM/ASP.NetPostURL.aspx


When I get Response.End()
the page loads and does not call the redirect method.

What am I trying to do?
I am trying to post some data from a .NET 1.1 page to a remote url
and afer the post, redirect to another page with the values from the form
page so I can do some data processing.

Any suggestions?
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string name = string.Empty;
        string email = string.Empty;
 
        if (txtName.Text != string.Empty)
        {
            name = txtName.Text;
        }
 
        if (txtEmail.Text != string.Empty)
        {
            email = txtEmail.Text;
        }
 
        RemotePost rp = new RemotePost();
        rp.Url = "http://somewebsite.com";
        rp.Add("visiblefields", "Name,Email1");
        rp.Add("visiblefields", "requiredfields");
        rp.Add("Name", name);
        rp.Add("Email1", email);
 
        rp.Post();
 
        Response.Redirect("ContactSave.aspx?name=" + name + "&email=" + email);
    }
 
 
public void Post()
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Write("");
        HttpContext.Current.Response.Write(string.Format("", FormName));
        HttpContext.Current.Response.Write(string.Format("", FormName, Method, Url));
        for (int i = 0; i < Inputs.Keys.Count; i++)
        {
            HttpContext.Current.Response.Write(string.Format("", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
        }
        HttpContext.Current.Response.Write("");
        HttpContext.Current.Response.Write("");
        HttpContext.Current.Response.End(); // once I get here everything stops
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of the_crazed
the_crazed
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

Thank you!
sorry, misunderstood what the RemotePost class is doing, it just writes out a form doesn't it? it does not actually post the data.
it looks to me like the author's just trying to write his own server.transfer.... seems odd, as this IS offered in .Net 1.1.
what he is doing is:
accept postback -> forward data (and user) to another page

but what you are attempting is:
accept postback -> forward data to another page -> forward user (+data) to a third page.

is that correct?

do you have control of the second page? if you did, I'd say the easiest would be to use Server.Transfer twice, and use the second page to forward the user on.

assuming you don't, you'll have to use XmlHttpRequest; here is a rewrite of RemotePost:

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.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
 
			string postData="";
			for(int i=0;i< Inputs.Keys.Count;i++)
			{
				postData += (postData.Length > 0 ? "&" : "") + Inputs.Keys[i] + "=" + Inputs[Inputs.Keys[i]];
			}
 
			byte[]  data = encoding.GetBytes(postData);
 
			System.Net.HttpWebRequest web = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url);
			web.PreAuthenticate = true;
			web.Credentials = System.Net.CredentialCache.DefaultCredentials;
			web.Method=Method;
			web.ContentType="application/x-www-form-urlencoded";
			web.ContentLength = data.Length;
			System.Net.WebResponse resp= web.GetResponse();
 
			Stream s = resp.GetResponseStream();
			StreamReader t = new StreamReader(s);
			
			string strResponse = t.ReadToEnd();
			//you can inspect strResponse to see if the page returned as expected}
}

Open in new window

Avatar of JRockFL

ASKER

Thanks for your reply again.
Yes, I am trying to post data from my aspx page, to a remote web page.
I need to capture the data the user has inputed and then query the db with the info they provided.

Is that what your rewrite will do?

I stepped through your code and when I get to
System.Net.WebResponse resp = web.GetResponse();
it steps out of the debugger and displays the page
Avatar of JRockFL

ASKER

On my aspx page I need to have this code...

<form name="form1" method="post" action="http://www.somewebsite.com/dostuff.asp">
<input name="merchantid" type="hidden" id="merchantid" value="xxxx">
<input name="ARThankyouURL" type="hidden" id="ARThankyouURL" value="www.this_can_be_a_page_at_my_site?merchantid=xxxx">
<input name="visiblefields" type="hidden" id="visiblefields" value="Name,Email1">
<input name="requiredfields" type="hidden" id="requiredfields" value="Name,Email1">
<table bgcolor="#FFFF99">
    <tr>
      <td>Name</td>
      <td><input name="Name" type="text" size="40"></td>
    </tr>
    <tr>
      <td>Email</td>
      <td><input name="Email1" type="text" size="40"></td>
    </tr>
      <tr align="center">
            <td colspan="2">
                  <input type="Submit" name="cmdSubmit" value="Submit">
            </td>
      </tr>
  </table>
</form>

So I pass the merchantid, and the textbox values "http://www.somewebsite.com/dostuff.asp"
This web site will do some stuff that I have no control over.
And they can redirect back to a page I specified. I may not need this since they are not doing any validation.
I just need to send them data and then do some processing on my end.
yes, this is what the rewrite should do.
what error do you get on the line
"System.Net.WebResponse resp = web.GetResponse();"
?

I note that in your original code you do not supply the merchant id - maybe this is the problem?
also, your passing of required fields look odd - I'd expect:
        ra.Add("merchantid", "xxxxx");
        rp.Add("visiblefields", "Name,Email1");
        rp.Add("requiredfields", "Name,Email1");
        rp.Add("Name", name);
        rp.Add("Email1", email);

Open in new window

Avatar of JRockFL

ASKER

Thanks for your help, i replaced the actual merchantid with xxxxx

I am playing around with it some more at home, i was at work and may of had some firewall issues.
If I have more questions, I will open a new question
Hi JRockFL,
 yes I thought you had, I just meant that in the original code you posted:

        RemotePost rp = new RemotePost();
        rp.Url = "http://somewebsite.com";
        rp.Add("visiblefields", "Name,Email1");
        rp.Add("visiblefields", "requiredfields");
        rp.Add("Name", name);
        rp.Add("Email1", email);
        rp.Post();

that merchantid was not mentioned.

Further to this, I'm beginning to think you'll save yourself problems if you alter your approach. might i suggest you use:
1) accept postback -> forward user + data to merchant page -> make merhchant forward user (+data) to a third page for data processing
OR even more simply
2) accept postback -> do data processing -> forward user + data to merchant page

the following code changes in your app will do number 1:

--rp call
rp.Add("merchantid", "xxxxx");
rp.Add("visiblefields", "Name,Email1");
rp.Add("requiredfields", "Name,Email1");
rp.Add("Name", name);
rp.Add("Email1", email);
rp.Add("ARThankyouURL","http://www.yoursite.com/ContactSave.aspx?name=" + name + "&email=" + email + "&merchantid=xxxx");
 
--RemotePost class
	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()
		{
			HttpContext.Current.Response.Clear();
			HttpContext.Current.Response.Write(string.Format("<form name='{0}' id='{0}' method='{1}' action='{2}'>", FormName, Method, Url));
			for (int i = 0; i < Inputs.Keys.Count; i++)
			{
				HttpContext.Current.Response.Write(string.Format("<input type='hidden' id='{0}' name='{0}' value='{1}'/>", Inputs.Keys[i], Inputs[Inputs.Keys[i]]));
			}
			HttpContext.Current.Response.Write("</form>");
			HttpContext.Current.Response.Write(string.Format("<script language='javascript'>document.{0}.submit()</script>",FormName));
			HttpContext.Current.Response.End(); 
		}
	}

Open in new window

Avatar of JRockFL

ASKER

Thank you so much for your help, I have things working and have a little better understanding of HttpRequest