How to post form variables to another URL using asp.net

D

DOUGLAS HEESTAND

How do you post to another URL (i.e. an external non-asp.net page). I
am trying to create a payment solution with authorize.net and they
require that you post to a URL they provide. I want to use asp.net to
take advantage of the nice form validation and other things but I
can't get it to post to anything but itself. If I use
"Response.Redirect()" it doesn't post the form variables,
"Server.Transfer()" doesn't work because it's not a file on my web
server.

Am I forced to use a standard html form?

thanks!
 
R

Raterus

I'm sure you've tried setting the action attribute on the <form
runat="server"> to the page you want, which won't work, because as soon as
you put runat="server" asp.net is going to change the action to the page
it's on to allow for the postback. If you take away runat="server", none of
your server controls can be within this form. Hmmm...

How do you get around this?, javascript :) On the button you are clicking
to send the info to the other server do this.

myButton.Attributes.Add("onClick", "javascript:
document.forms[0].action='http://otherserver.com/theirpage.html';document.fo
rms[0].__VIEWSTATE=''")

You may want to clear out the viewstate hidden form field too, since the
other page won't be able to do much with it, and no sense making your client
send more than they need to.

Don't you submit the form yourself though, just change the action. Let
asp.nets __doPostBack() function handle the posting (this will ensure your
validators fire too). Asp.net never has to know you changed the
action..heehee.

I'm assuming this will work (maybe not the javascript since I just wrote
that by hand), though I've never actually tried this. Let me know how it
works!

--Michael
 
D

DOUGLAS HEESTAND

It worked!! What a brilliant idea! I had to do some modifications
because it didn't work if I had form validation as well. (That places
a javascript call on the "onClick" event of the submit button too, so
I couldn't get the two to work in the proper sequence.) So instead, I
attached your idea to the onSubmit property of the form itself...

<script language="VB" runat="server">
Sub btnSubmit_Click(Sender As Object, E As EventArgs)
myform.Attributes.Add("onSubmit",
"javascript:document.forms[0].action='http://otherserver.com/theirpage.html';")
End Sub
</script>

Thanks so much!! Do you think this is a risky approach because some
browsers might not handle the javascript properly??

Raterus said:
I'm sure you've tried setting the action attribute on the <form
runat="server"> to the page you want, which won't work, because as soon as
you put runat="server" asp.net is going to change the action to the page
it's on to allow for the postback. If you take away runat="server", none of
your server controls can be within this form. Hmmm...

How do you get around this?, javascript :) On the button you are clicking
to send the info to the other server do this.

myButton.Attributes.Add("onClick", "javascript:
document.forms[0].action='http://otherserver.com/theirpage.html';document.fo
rms[0].__VIEWSTATE=''")

You may want to clear out the viewstate hidden form field too, since the
other page won't be able to do much with it, and no sense making your client
send more than they need to.

Don't you submit the form yourself though, just change the action. Let
asp.nets __doPostBack() function handle the posting (this will ensure your
validators fire too). Asp.net never has to know you changed the
action..heehee.

I'm assuming this will work (maybe not the javascript since I just wrote
that by hand), though I've never actually tried this. Let me know how it
works!

--Michael

DOUGLAS HEESTAND said:
How do you post to another URL (i.e. an external non-asp.net page). I
am trying to create a payment solution with authorize.net and they
require that you post to a URL they provide. I want to use asp.net to
take advantage of the nice form validation and other things but I
can't get it to post to anything but itself. If I use
"Response.Redirect()" it doesn't post the form variables,
"Server.Transfer()" doesn't work because it's not a file on my web
server.

Am I forced to use a standard html form?

thanks!
 
D

DOUGLAS HEESTAND

After more fiddling it seems I can't get it to co-exist with form
validation. Changing the value of the "onSubmit" property of the form
causes the last validation to be bypassed. If I try to change the
value of the "onClick" property of the submit button I get something
like this when it is rendered into HTML:

<input type="submit" name="btnSubmit" value="Submit"
onclick="javascript:document.forms[0].action='http://othersite.com/otherform.html;if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btnSubmit" />

So it submits before validating and I can't make it validate first. I
could create a custom javascript function but that is starting to get
hokey and just plain ugly. I'd rather use a vanilla html form at that
point!

I can't believe ASP.NET can't handle this situation!

-Doug
 
A

Alessandro Zifiglio

hi Douglas,

Below i am providing you with a quick copy and paste from the docs. For a
working example of how this can work for you, go to :
http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=PayPal


Requests and Responses in the .NET Framework
The .NET Framework uses specific classes to provide the three pieces of
information required to access Internet resources through a request/response
model: the Uri class, which contains the URI of the Internet resource you
are seeking; the WebRequest class, which contains a request for the
resource; and the WebResponse class, which provides a container for the
incoming response.

Client applications create WebRequest instances by passing the URI of the
network resource to the WebRequest.Create method. This static method creates
a WebRequest instance for a specific protocol, such as HTTP. The WebRequest
instance that is returned provides access to properties that control both
the request to the server and access to the data stream that is sent when
the request is made. The GetResponse method on the WebRequest instance sends
the request from the client application to the server identified in the URI.
In cases in which the response might be delayed, the request can be made
asynchronously using the BeginGetResponse method on the WebRequest instance,
and the response can be returned at a later time using the EndGetResponse
method.

The GetResponse and EndGetResponse methods return a WebResponse instance
that provides access to the data returned by the server. Because this data
is provided to the requesting application as a stream by the
GetResponseStream method, it can be used in an application anywhere data
streams are used.

The WebRequest and WebResponse classes are the basis of pluggable
protocols-an implementation of network services that enables you to develop
applications that use Internet resources without worrying about the specific
details of the protocol that each resource uses. Descendant classes of
WebRequest are registered with the WebRequest class to manage the details of
making the actual connections to Internet


As an example, the HttpWebRequest class manages the details of connecting to
an Internet resource using HTTP. By default, when the WebRequest.Create
method encounters a URI that begins with "http:" or "https:" (the protocol
identifiers for HTTP and secure HTTP), the WebRequest instance that is
returned can be used as is, or it can be typecast to HttpWebRequest to
access protocol-specific properties. In most cases, the WebRequest instance
provides all the necessary information for making a request.

Any protocol that can be represented as a request/response transaction can
be used in a WebRequest. You can derive protocol-specific classes from
WebRequest and WebResponse and then register them for use by the application
with the static WebRequest.RegisterPrefix method.

When client authorization for Internet requests is required, the Credentials
property of the WebRequest supplies the necessary credentials. These
credentials can be a simple name/password pair for basic HTTP or digest
authentication, or a name/password/domain set for NTLM or Kerberos
authentication. One set of credentials can be stored in a NetworkCredentials
instance, or multiple sets can be stored simultaneously in a CredentialCache
instance. The CredentialCache uses the URI of the request and the
authentication scheme that the server supports to determine which
credentials to send to the server.





DOUGLAS HEESTAND said:
After more fiddling it seems I can't get it to co-exist with form
validation. Changing the value of the "onSubmit" property of the form
causes the last validation to be bypassed. If I try to change the
value of the "onClick" property of the submit button I get something
like this when it is rendered into HTML:

<input type="submit" name="btnSubmit" value="Submit"
onclick="javascript:document.forms[0].action='http://othersite.com/otherform
..html;if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btnSubmit" />

So it submits before validating and I can't make it validate first. I
could create a custom javascript function but that is starting to get
hokey and just plain ugly. I'd rather use a vanilla html form at that
point!

I can't believe ASP.NET can't handle this situation!

-Doug
 
A

Alessandro Zifiglio

and on a final note, if authorize.net is providing you with payment
solutions then they should have some code example as to how you can connect
and what you need to do. Try contacting them if this information is not
available to you on their faqs page etc.

Alessandro Zifiglio said:
hi Douglas,

Below i am providing you with a quick copy and paste from the docs. For a
working example of how this can work for you, go to :
http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=PayPal


Requests and Responses in the .NET Framework
The .NET Framework uses specific classes to provide the three pieces of
information required to access Internet resources through a request/response
model: the Uri class, which contains the URI of the Internet resource you
are seeking; the WebRequest class, which contains a request for the
resource; and the WebResponse class, which provides a container for the
incoming response.

Client applications create WebRequest instances by passing the URI of the
network resource to the WebRequest.Create method. This static method creates
a WebRequest instance for a specific protocol, such as HTTP. The WebRequest
instance that is returned provides access to properties that control both
the request to the server and access to the data stream that is sent when
the request is made. The GetResponse method on the WebRequest instance sends
the request from the client application to the server identified in the URI.
In cases in which the response might be delayed, the request can be made
asynchronously using the BeginGetResponse method on the WebRequest instance,
and the response can be returned at a later time using the EndGetResponse
method.

The GetResponse and EndGetResponse methods return a WebResponse instance
that provides access to the data returned by the server. Because this data
is provided to the requesting application as a stream by the
GetResponseStream method, it can be used in an application anywhere data
streams are used.

The WebRequest and WebResponse classes are the basis of pluggable
protocols-an implementation of network services that enables you to develop
applications that use Internet resources without worrying about the specific
details of the protocol that each resource uses. Descendant classes of
WebRequest are registered with the WebRequest class to manage the details of
making the actual connections to Internet


As an example, the HttpWebRequest class manages the details of connecting to
an Internet resource using HTTP. By default, when the WebRequest.Create
method encounters a URI that begins with "http:" or "https:" (the protocol
identifiers for HTTP and secure HTTP), the WebRequest instance that is
returned can be used as is, or it can be typecast to HttpWebRequest to
access protocol-specific properties. In most cases, the WebRequest instance
provides all the necessary information for making a request.

Any protocol that can be represented as a request/response transaction can
be used in a WebRequest. You can derive protocol-specific classes from
WebRequest and WebResponse and then register them for use by the application
with the static WebRequest.RegisterPrefix method.

When client authorization for Internet requests is required, the Credentials
property of the WebRequest supplies the necessary credentials. These
credentials can be a simple name/password pair for basic HTTP or digest
authentication, or a name/password/domain set for NTLM or Kerberos
authentication. One set of credentials can be stored in a NetworkCredentials
instance, or multiple sets can be stored simultaneously in a CredentialCache
instance. The CredentialCache uses the URI of the request and the
authentication scheme that the server supports to determine which
credentials to send to the server.





DOUGLAS HEESTAND said:
After more fiddling it seems I can't get it to co-exist with form
validation. Changing the value of the "onSubmit" property of the form
causes the last validation to be bypassed. If I try to change the
value of the "onClick" property of the submit button I get something
like this when it is rendered into HTML:

<input type="submit" name="btnSubmit" value="Submit"
onclick="javascript:document.forms[0].action='http://othersite.com/otherform
.html;if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btnSubmit" />

So it submits before validating and I can't make it validate first. I
could create a custom javascript function but that is starting to get
hokey and just plain ugly. I'd rather use a vanilla html form at that
point!

I can't believe ASP.NET can't handle this situation!

-Doug
 
S

Scott Hill

This didnt work for me. It always gives my a javascript error.

error: "Microsoft JScript runtime error: Object doesn't support this
property or method"


Here is the code I am trying:

protected System.Web.UI.WebControls.Button butSubmit;

butSubmit.Attributes.Add("onClick",

"javascript:document.forms[0].action='http://localhost/bpass/test3.aspx'
;document.forms[0].__VIEWSTATE=''");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top