Send data from non browser client (using HttpWebRequest) that can be used by access web page controls.

 If you use HttpWebRequest to send request via Form, that contain server side controls names and values, to the server you except server control to hold the values that send from the client. Exactly as you send data using browser to the server. The following code should send data to Textbox1 and TextBox2 and if I set a bookmark inside page load I should access the textboxes value using textbox text property

<CODE>

    Sub Main()

        Dim URL As String = "http://localhost/webapplication36/webform1.aspx?"

        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(URL), HttpWebRequest)

        myHttpWebRequest.Method = "POST"

        Dim postData As String = "TextBox1=myusername&TextBox2=mypassword"

        Dim encodedData As New System.Text.UTF8Encoding

        Dim byteArray As Byte() = encodedData.GetBytes(postData)

        myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

        Dim cont As CookieContainer = New CookieContainer

        'myHttpWebRequest.CookieContainer = New CookieContainer

        'myHttpWebRequest.CookieContainer.Add(cont.GetCookies(New System.Uri(URL)))

      

        myHttpWebRequest.ContentLength = byteArray.Length

        Dim newStream As Stream = myHttpWebRequest.GetRequestStream()

        newStream.Write(byteArray, 0, byteArray.Length)

        newStream.Close()

        myHttpWebRequest.AllowAutoRedirect = True

        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

        'read response

         . . .

}

</CODE>

The problem is that this code won't cause TextBox1 and TextBox2 to hold the submitted form data.

The webform won't add the submitted values to controls because the form is missing __VIEWSTATE hidden form field. From the page point of view request that arrive to the server without __VIEWSTATE field are first time request to given page. While page call for the first time there isn’t any values to preserve, on the contrary one of the page tasks is to save controls state into __VIEWSTATE field. From the page point of view if __VIEWSTATE field isn’t exist ASP.NET won't load controls and set their values.

So the trivial solution is to add "&__VIEWSTATE=" to the form data submitted from the client. The problem is that adding this data will cause your web to return error to the client with error description saying that the ViewState data is corrupted.

<CODE>

Dim postData As String = "TextBox1=myusername&TextBox2=mypassword&Button1=Button&__VIEWSTATE="

Dim encodedData As New System.Text.UTF8Encoding

Dim byteArray As Byte() = encodedData.GetBytes(postData)

</CODE>

Actually the problem is within page LoadPageStateFromPersistenceMedium method which responsible to load view state from __ViewState field. The Request holds indication that ViewState data sent so the page. The page tries to persist data from __ViewState field but __ViewState field hold nothing or garbage.

To fix this problem you can overload LoadPageStateFromPersistenceMedium in such a way that if the __ViewState field holds corrupted data (or empty data, you can check the Form value)  the page life cycle will continue. This way given page can serve browser and HttpWebRequest requests :

<CODE>

protected override object LoadPageStateFromPersistenceMedium()

{

      object oViewState = null;

      try

      {

            /*

            // can check __VIEWSTATE –

            if(Request.Form["__VIEWSTATE"] != "")

                  return base.LoadPageStateFromPersistenceMedium();

            */         

            oViewState = base.LoadPageStateFromPersistenceMedium();

           

      }

      catch(Exception HttpException)

      {

           

      }

      return oViewState;

}

</CODE>

2 Comments

  • Could you please tell me what is 'base' in this code. I am trying to implement this solution in a ASP.NET application and having trouble returning Form values if __VIEWSTATE is empty.

  • you mean &quot;return base.LoadPageStateFromPersistenceMedium();&quot; if so its the base Form class.



    I you want email me to natty(at)dao2com(dot)com and i'll send you back the complete source.

Comments have been disabled for this content.