• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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 ;)
0
Dwalden
Asked:
Dwalden
  • 3
  • 2
1 Solution
 
PedroGCommented:
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
0
 
DwaldenAuthor Commented:
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?
0
 
PedroGCommented:
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.Clear();
  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("</form>")
  Response.Write("<script language=""javascript"">")
  Response.Write("testForm.submit();");
  Response.Write("</script>");

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



0
 
DwaldenAuthor Commented:
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.
0
 
PedroGCommented:
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)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now