0

With the code below I can request a page with post parameters. But the page requested couldn't get the parameters. I put two textboxes to the requested page and send parameters to this page. What is wrong with this code?

private string PostForm(string _targetUrl, string _parameter1, string _parameter2)
{

    WebRequest request = WebRequest.Create(_targetUrl);

    request.Method = "POST";

    request.ContentType = "application/x-www-form-urlencoded";

    string postContent = string.Format("Textbox1={0}&Textbox2={1}", _parameter1, _parameter2);

    byte[] postContentBytes = Encoding.ASCII.GetBytes(postContent);

    request.ContentLength = postContentBytes.Length;

    Stream writer = request.GetRequestStream();

    writer.Write(postContentBytes, 0, postContentBytes.Length);

    writer.Close();

    HttpWebResponse testResponse = (HttpWebResponse)request.GetResponse();

    if (!testResponse.StatusDescription.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
    {
        Response.Write("Error");
    }

    StreamReader sr = new StreamReader(testResponse.GetResponseStream());
    string returnvalue = sr.ReadToEnd();

    return returnvalue;
}
3
  • You should post the form also. Commented Oct 30, 2012 at 10:22
  • How can I post the requested page's form? For sample I didn't put a button to the page. Should I put a submit button and call it? If yes how? Commented Oct 30, 2012 at 10:32
  • I can read parameters with Request.Form["Textbox1"]. But the page can't set textbox value as soon as page requested. Commented Oct 30, 2012 at 10:44

1 Answer 1

1

Make sure that "name" of textbox inputs on the target page are Textbox1 and Textbox2.

Sign up to request clarification or add additional context in comments.

3 Comments

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:textbox ID="Textbox1" runat="server"></asp:textbox> <asp:textbox ID="Textbox2" runat="server"></asp:textbox> </asp:Button> </div> </form> </body> </html>
You want the page to display with values in textbox inputs? You can't do like this (viewstates, aspnet validation...). But I don't understand what you want to do...
It is a sample form. I want to send parameters to an external url and submit that form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.