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

Posted on 2003-02-28
Medium Priority
Last Modified: 2008-03-10
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);
               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 ;)
Question by:Dwalden
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 8044896

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

Author Comment

ID: 8045286
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?

Accepted Solution

PedroG earned 300 total points
ID: 8051109
You just need to change the action value in the HTML.

You should have something like <FORM ID="form1" METHOD="post">.

You must add an action tag with the url of the destination
<FORM ID="form1" METHOD="post" ACTION="https://www.somesite.com/cgi-bin/somescrpt">

There's only one consideration that you should have is that in this case you lose post back handling.

If post back handling is a must you have one option that is
something like this

private void btnPay_Click(object sender, System.EventArgs e)
  Response.Write("<form name=""testForm"" method=""post"" action=""https://www.somesite.com/cgi-bin/somescrpt"">");
  Response.Write("<input type=""hiden"" name=""field"" value=""test"">");
// add all your fields
  Response.Write("<script language=""javascript"">")

Using this approach your sendding back to the user a filled form that will automaticaly be submited (therefore real redirecting the browser).


Author Comment

ID: 8051351
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.

Expert Comment

ID: 8052804
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)

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question