Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to access the webpage programmatically. I do have the authorized account as well as the valid username and password. However,i run into problem of getting the access by passing the valid username and password using screen scraping so that i cannot do the further steps. I don't know which part of my code goes wrong.Please help.Below is my code




myReq2.Method = "POST"
myReq2.ContentLength = postData.ToString.Length
myReq2.ContentType = "application/x-www-form-urlencoded"
myReq2.CookieContainer = cookies
myReq2.KeepAlive = True

Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myReq2.GetRequestStream())
sw.Write(postData)
sw.Close()

Dim myResp2 As System.Net.HttpWebResponse = CType(myReq2.GetResponse(), System.Net.HttpWebResponse)

Dim sr2 As System.IO.StreamReader = New System.IO.StreamReader(myResp2.GetResponseStream())

resp2 = sr2.ReadToEnd
sr2.Close()

Private Function ExtractViewState(ByVal s As String) As String

Return System.Web.HttpUtility.UrlEncodeUnicode(System.Text.RegularExpressions.Regex.Match(s, "(?<=__VIEWSTATE"" value="")(?<val>.*?)(?="")").Groups("val").Value)


End Function
Posted
Updated 3-May-13 10:28am
v7
Comments
ZurdoDev 2-May-13 16:51pm    
I think you need to refer to the google api docs.

1 solution

I think the content of the stream that you post is not correct, you need to ASCII encoded the string before adding it to your request.
See the modifications to your code here:
VB
Dim postString As String
postString = "use...=<>&pass...=<>" ' insert the name-value pairs to post here

Dim postData As Byte()                         ' you are going to post encoded bytes

postData = Encoding.UTF8.GetBytes(postString) ' convert the string to the UTF8 encoded bytes

Dim myReq2 As System.Net.HttpWebRequest = System.Net.WebRequest.Create(roServerSettings.GET_MSI_URL & "Login.aspx")

myReq2.Method = "POST"
myReq2.ContentLength = postData.Length         ' The length of the encoded bytes
myReq2.ContentType = "application/x-www-form-urlencoded"
myReq2.CookieContainer = cookies
myReq2.KeepAlive = True
 
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(myReq2.GetRequestStream())
sw.Write(postData)
sw.Close()


This should get the string correctly encoded to the other side.
If you still get an error, check, if you are sending correctly formatted name-value pairs as per the API specification.
 
Share this answer
 
v2
Comments
vidkaat 3-May-13 6:20am    
Steve thanks for the solution . I will try. Also i am using a Viewsstate in postData like this
postData = "__VIEWSTATE=" & vState & "&ddlRegion=" & merchantID.Substring(2, 2) & "&txtPassword=%24%24region15%23%23&btnEnter=Enter".........Can there be an issue with the ViewState ...If so how can i validate it?Please help....Appreciate your time
Steve44 3-May-13 9:45am    
Regarding the __VIEWSTATE, that is an information you would have to retrieve from the API specification. Unfortunately I never worked with the gmail API and can't provide more details.
vidkaat 3-May-13 9:57am    
Thanks. Its not gmail API it is a different site. But generally how can we find the API specification. When i google i came to know about Fiddler

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900