Posting data to other sites

  • Thread starter Thread starter Paul Hodgson
  • Start date Start date
P

Paul Hodgson

The web site I'm working on needs to interact with another site, whose
services we are using. This means that we need need to post some form data
to a secure web page on the remote site, and then get a reply back. This was
easy to do in ASP - you just did <form method="post"
action="Url_of_the_other_site"> etc. But I'm getting stuck on how to do this
in ASP.NET because ASP.NET insists that a form posts back to itself.

Is there any way to have an ASP.NET page send post data to a different page?

Simon
 
You can always write simple html in ur aspx file :) ... if none of the
elements in form requires event handling on server, u dont need to use
runat="server" anywhere in ur form definition , and you can post it to any
location u want to...

Abhijeet Dev
 
Unfortunately you can't use the validators if you don't use a runat="server"
tag, or any other asp.net server control for that matter.

I've thought of one way, but never tried it out because I haven't needed to.
My idea though was to write some javascript to change the form's target when
you click the desired button.
 
Hi Simon,

As Raterus and Abhijeet have suggested. We can make the ASP.NET page's
<form > tag as non runat=server so as to make it able to posted to another
page(just as a normal html page). However, using this means will lose the
features of the ASP.NET pages.

In addition to the above means, here are some other approaches:
1. Some some clientside script to change the form's tag's attributes when
the form's submit event is fired. But if the targe page we want to post to
is also an ASP.NET page, we'd consider some issues with the validation and
viewstate checking of the ASP.NET page. Here is a tech article on using
javascript to post a ASP.NET form:

#Post an ASP.NET form with JavaScript
http://www.codeproject.com/aspnet/jsnopostback.asp

2. If the post operation (the datas want to post) can be generate all via
code, you may also consider using the webrequest class to do the post ,
here are some references on using the webrequest class to post to a certain
url:

#Requesting Data
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconrequestingdata.asp
?frame=true

#POSTing Data with ASP.NET
http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp

#How Do I...Make a POST request?
http://samples.gotdotnet.com/quickstart/howto/doc/WebRequests/clientPOST.asp
x

Hope these help.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
I had this exact same problem while working on a project a while back too.
I reseached 2 ways to resolve this problem:
1. Handle the postback on the server, then do your own WebRequest post to
the remote
server, then get the response and then send it back to the client.
2. I sent a response page back to the client with a single form and form
entries, and put javascript code in the response page to automatically
redirect the user to go contact the remote server.

#1 is feasible, but too complicated for what I was doing. It also didn't
make sense as my server would be just be the "middle man" in the 3 way
communcation. This solution just didn't seem to be efficient, so I didn't
pursue this option any further.

#2 was the one I choose to do. This one was easy todo as the
request/response occurs normally. Once the postback occurs, I performed some
calucations, then returned a page with the form values and the javascript
code to redirect the user. Since my form variables can change depending on
the input from the user, this one was also better suited as I can customize
these values before it is sent back to the user.

Eric
 
Hi Barbara,

When we using the javascript to post from one aspx page to another aspx
page. All the html input element's value will be contained in the Request's
Form data collection and be transfered to the destination page. For
example, if we have t he following form content in the first page:
<form....>
<input type="text" name="Name" value="MyName" />
<input type="text" name="Email" value="MyEmail" />
<input type="hidden" name="LoginID" value="343434" />
</form>

Then, when this page is posted to another aspx page, we can retrieve these
information in the Page_Load event as following:

private void Page_Load(object sender, System.EventArgs e)
{

string name = Request.Form["Name"];
string email = Request.Form["Email"];
string id = Request.Form["LoginID"];
}

also, you can use the following foreach loop to loop through all the datas
contained in the Request's Form collection:
foreach(string key in Request.Form.Keys)
{
Response.Write("<br>" + key + " : " + Request.Form[key]);
}


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
I had been opening the newsgroup through the browser which doesn't allow attachments. I now opened it through my outlook and retrieved the files. I'm looking at them now. Let you know how it works ou

Thank-you
Barbara Alderton
 
Back
Top