0

I am bit confused in using httpwebrequest. I tried to look into some articles but not able to get much from it as I am doing it for the first time. Below is the code I am trying to work on and I have few questions,

a) The ASPX page has few controls defined and in code-behind I create few controls. When I do httpwebrequest with POST, do I need to consider all controls and their values? I need to do POST for one of the controls only. Can I do it only for that control?

b) What URL should be specified in "(HttpWebRequest)WebRequest.Create"? I assume it is the same page that is shown to the user. For example in my example below it is ("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");

c) Is there anything else I need to modify or take care of either in markup or code to achieve httpwebrequest?

    private void OnPostInfoClick(object sender, System.EventArgs e)
{
    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData =  ""; //Read from the stored array and print each element from the array loop here. 
    byte[] data = encoding.GetBytes(postData);

    // Prepare web request...
    HttpWebRequest myRequest =
      (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");
    myRequest.Method = "POST";
    myRequest.ContentType = "application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream = myRequest.GetRequestStream();
    // Send the data.
    newStream.Write(data, 0, data.Length);
    newStream.Close();
}
NewCoder
  • 99
  • 4
  • 13

1 Answers1

1

You typically would only use it for server-to-server communication (not necessarily for page to page data transfer).

If I'm understanding your post and question correctly, you just seem to want to send POST data to some other page in your ASP.Net application. If so, one way you can do that is to simply change the PostBackUrl of your submit button (form target) instead of the normal Postback (to same page).

There are other ways, but this should be the simplest.

<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="foo.aspx" />

In the above, instead of POSTing back to itself, the POST will be sent to foo.aspx where you can examine/use/process the POSTed data.


Update based on your comment:

You don't have to code your way through HttpWebRequest for that. The normal ASP.net WebForms model does it for you.

Given this simple ASP.net web Forms page:

<form id="form1" runat="server">

Coffee preference:
<asp:RadioButtonList ID="CoffeeSelections" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
   <asp:ListItem>Latte</asp:ListItem>
   <asp:ListItem>Americano</asp:ListItem>
   <asp:ListItem>Capuccino</asp:ListItem>
   <asp:ListItem>Espresso</asp:ListItem>
</asp:RadioButtonList>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

....
</form>

Your inline or code behind would look something like this:

protected void Page_Load(object sender, EventArgs e)
{
   //do whatever you want to do on Page_Load
}


protected void Button1_Click(object sender, EventArgs e)
{
   //handle the button event
   string _foo = CoffeeSelections.SelectedValue;
   ...
}
EdSF
  • 11,753
  • 6
  • 42
  • 83
  • thanks for the reply. No I am not trying to post to another page but it the same page. Basically in Onload of the page, I call the httpwebrequest and I want the same page to be displayed with the POST data. – NewCoder Jun 04 '12 at 15:00
  • Then there is nothing "extra" for you to do. You can handle the Postback data (which is the default asp.net web forms behavior). – EdSF Jun 04 '12 at 15:01
  • So in the above example, how do I take of following: I have an image control that is generated at runtime and the imageUrl is too long. Due to this, I am going for POST approach. however when page is displayed, will it show the image correctly? Where in the above code, I need to mention that image-control? – NewCoder Jun 04 '12 at 15:10
  • Updated the answer to show how you'd handle POSTed data (one way). You can also inspect the Request data if you prefer. Provide more detail on your "image url too long" or create a new question if the above concept doesn't quite get you there... – EdSF Jun 04 '12 at 15:17
  • Thanks but here is what I am doing. In OnLoad() I create imageControl and assign to it the URL from XML file. I then parse the URL and still find that URL length is longer (more than 3000 characters). I then create an array and the array elements are basically the values from URL with each "&" an array element. I then loop that array and create POSTDATA in the above httpwebrequest code. So my confusion is when the POST data is done and my page is displayed, how will the image get displayed and what will the URL value for the image? – NewCoder Jun 04 '12 at 15:27
  • Unsure I understand - what would the extra POST be doing? In other words, why do you need to POST somewhere first before rendering the page? Are you trying to parse the (long) url and only obtain the relevant part to - re: to assign to the image src? – EdSF Jun 04 '12 at 16:02
  • Yes. whole goal is to make sure the imageURL gets rendered. So I thought of doing POST to acheive this. I create the imagecontrol and assign the Url in OnInit or ONload – NewCoder Jun 04 '12 at 16:27
  • Anyone has suggestions on what I am doing? – NewCoder Jun 04 '12 at 16:56
  • This comment routine isn't good - ask a new question because it is. You can also review, reference/search previous [SO questions like this one](http://stackoverflow.com/q/10800610/304683) – EdSF Jun 04 '12 at 17:07