WebClient VB Post Data to Webpage

B

Ben

Hi

I have the two funcions below, login_web that posts data to a webpage and
fcn_parseviewstate that parses the viewstate data for the post.

I have a problem with the strPostData, I have been testing this using a HTTP
Debugging Proxy, when I post the page manually using a browser it posts the
below data and is successful, if I copy this data into strPostData and post
it using the code below it posts successfully:
strPostData =
"__VIEWSTATE=dDw0NzU3NTEyMjk7Oz4eJhsIFXbxP7On1dC%2BEy1wFpRZKg%3D%3D&txtCmd=&txtData=INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+%28%27description%27%29&post=Button"

Broken Down into:
__VIEWSTATE=dDw0NzU3NTEyMjk7Oz4eJhsIFXbxP7On1dC%2BEy1wFpRZKg%3D%3D
txtCmd=
txtData=INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+%28%27description%27%29
post=Button

BUT
My when my program posts the data normally (without copying the above data)
program posts the data in this format:
......
txtData=INSERT INTO tblMatters (MatterDescription) VALUES ('TESTING')
......

This means that the post fails...

How can i convert INSERT INTO tblMatters (MatterDescription) VALUES
('TESTING') TO
INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+%28%27description%27%29

Thanks

B

Private Function web_login() As Boolean
On Error GoTo err_web_login

Dim strURL As String

Dim strPostData As String

Dim byteResponse() As Byte

Dim i As Integer

Dim strViewState As String

strURL = strHttpLocation & "sync.aspx?u=simonfinn&p=1234"



REM ** Download Viewstate Data

byteResponse = gobjWebClient.DownloadData(strURL)

strViewState = System.Text.Encoding.ASCII.GetString(byteResponse)

strViewState = fcn_ParseViewState(strViewState)



'strPostData = "VIEWSTATE=" & strViewState & "&txtCmd=&txtData=" & "INSERT
INTO tblMatters (MatterDescription) VALUES ('TESTING')" & "&post=Button"

strPostData = "VIEWSTATE=" & strViewState & "&txtCmd=&txtData=" & "INSERT
INTO tblMatters (MatterDescription) VALUES ('TESTING')" & "&post=Button"

gobjWebClient.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")

byteResponse = gobjWebClient.UploadData(strURL, "POST",
System.Text.Encoding.ASCII.GetBytes(strPostData))

TextBox1.Text = System.Text.Encoding.ASCII.GetString(byteResponse)

web_login = True



REM ** err_web_login

Exit Function

err_web_login:

'Return Error

web_login = False

End Function



Private Function fcn_ParseViewState(ByVal strSource As String) As String

REM ** Declarations

Dim intViewStatePosition As Integer

Dim intViewStateEndPosition As Integer

REM ** Parse Viewstate Start Posision + 20 Chars for End

intViewStatePosition = strSource.IndexOf("__VIEWSTATE") + 21

REM ** Parse Viewstate End Posision

intViewStateEndPosition = Mid(strSource, intViewStatePosition).IndexOf("
/") - 1

REM ** Return Viewstate only

fcn_ParseViewState = Mid(strSource, intViewStatePosition,
intViewStateEndPosition)

End Function
 
S

Sergey Poberezovskiy

You can use System.Web.HttpUtility.HtmlEncode Shared
function to produce the string ready to be transmitted
via Http

-----Original Message-----
Hi

I have the two funcions below, login_web that posts data to a webpage and
fcn_parseviewstate that parses the viewstate data for the post.

I have a problem with the strPostData, I have been testing this using a HTTP
Debugging Proxy, when I post the page manually using a browser it posts the
below data and is successful, if I copy this data into strPostData and post
it using the code below it posts successfully:
strPostData =
"__VIEWSTATE=dDw0NzU3NTEyMjk7Oz4eJhsIFXbxP7On1dC% 2BEy1wFpRZKg%3D%
3D&txtCmd=&txtData=INSERT+INTO+tblMatters+%
28matterdescription%29+VALUES+%28%27description%27%
29&post=Button"

Broken Down into:
__VIEWSTATE=dDw0NzU3NTEyMjk7Oz4eJhsIFXbxP7On1dC% 2BEy1wFpRZKg%3D%3D
29+VALUES+%28%27description%27%29
post=Button

BUT
My when my program posts the data normally (without copying the above data)
program posts the data in this format:
......
txtData=INSERT INTO tblMatters (MatterDescription) VALUES ('TESTING')
......

This means that the post fails...

How can i convert INSERT INTO tblMatters (MatterDescription) VALUES
('TESTING') TO
INSERT+INTO+tblMatters+%28matterdescription%29+VALUES+% 28%27description%27%29

Thanks

B

Private Function web_login() As Boolean
On Error GoTo err_web_login

Dim strURL As String

Dim strPostData As String

Dim byteResponse() As Byte

Dim i As Integer

Dim strViewState As String

strURL = strHttpLocation & "sync.aspx?u=simonfinn&p=1234"



REM ** Download Viewstate Data

byteResponse = gobjWebClient.DownloadData(strURL)

strViewState = System.Text.Encoding.ASCII.GetString (byteResponse)

strViewState = fcn_ParseViewState(strViewState)



'strPostData = "VIEWSTATE=" & strViewState
& "&txtCmd=&txtData=" & "INSERT
INTO tblMatters (MatterDescription) VALUES ('TESTING')" & "&post=Button"

strPostData = "VIEWSTATE=" & strViewState
& "&txtCmd=&txtData=" & "INSERT
 
B

Ben

Thanks

That worked perfectly

B


Sergey Poberezovskiy said:
You can use System.Web.HttpUtility.HtmlEncode Shared
function to produce the string ready to be transmitted
via Http


& "&txtCmd=&txtData=" & "INSERT
& "&txtCmd=&txtData=" & "INSERT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top