Link to home
Start Free TrialLog in
Avatar of Dwalden
Dwalden

asked on

Using HttpWebRequest to transfer a user to a remote site with form data

Greetings,
I'm having trouble understanding the usages of HttpWebRequest to send a user to a remote website, while isuing the POST method to submit their responses as well.

Everything else in the aspx form works fine however when they click the submit button, nothing happens as far as I can tell. Following is the code for my submit routine:

          private void btnPay_Click(object sender, System.EventArgs e)
          {
               string cmdString = "var1=something&var2=something";


               textURLString.Text = cmdString;
               HttpWebResponse result = null;
               HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create("https://www.somesite.com/cgi-bin/somescrpt");
               req.Method = "POST";
               req.ContentType = "application/x-www-form-urlencoded";

               byte[] SomeBytes = null;
               SomeBytes = Encoding.UTF8.GetBytes(cmdString);
               req.ContentLength = SomeBytes.Length;
               Stream newStream = req.GetRequestStream();
               newStream.Write(SomeBytes, 0, SomeBytes.Length);
               newStream.Close();
               result = (HttpWebResponse) req.GetResponse();
          }

I can cheat and do this via a Response.Redirect(), but this exposes the variables sent in the address box. Which I don't want to do. And I really need to learn how to do this myself. Any help is appreciated, especially detailed help ;)
Avatar of PedroG
PedroG

Dwalden,

I don't think you can do what you whant...

Wen you use HTTPWebRequest class you'r creating and HTTP request initiated by the Web Server.

You can send the response bytes received by the new request to the user but this won't redirect him to the new site.

You must understand that the Redirect command is only a HTTP Header that is sent by the server to the client browser saying to get a new page.

The only way you can solve your problem is to have a form in the page with the action set to https://www.somesite.com/cgi-bin/somescrpt
Avatar of Dwalden

ASKER

Okay it's entirely possible the code examples I've been reading sent me down an entirely wrong path.

Then how do I get a Webform to act like a regular <form></form> and use a POST?
ASKER CERTIFIED SOLUTION
Avatar of PedroG
PedroG

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 Dwalden

ASKER

Sorry for my late reply PedroG, I didn't see an email saying I had a resposne

Whenever I try adding an action to the html of the .aspx file, the form does a postback anyways. Which I must admit is frustrating me incredibly.

I'll try the second way using Response.Write shortly when I get a chance and hopefully that method will work.

But do you have any idea why a C# .aspx file would ignore the action parameter in the html? In the btnPay_click event, the only thing called is a single function to do some database processing.
I think that due to the fact taht you have one event handler probably the post back code overwrites the html.

Try to remove the btnPay_Click event code (it only works with postback)