[TUT]getting reCaptcha image in picturebox[TUT] - jangedoo - Jun 7, 2011
here is a tutorial for getting reCaptcha image into a picture box.
Ok lets get to the point of getting reCaptcha image.
1. first download the page using httpwebrequest
2. then parse the reCaptcha url
3. goto that challenge url and you'll find the actual "key" for that image
4. parse that key and goto http://www.google.com/recaptcha/api/image?c=PARSED_KEY to get the image
Code Dim request As HttpWebRequest = HttpWebRequest.Create("www.pagewithcaptcha.com")
Dim response as HttpWebResponse
response = CType(request.GetResponse, HttpWebResponse)
Dim webstream As Stream = response.GetResponseStream
Dim streamreader As New StreamReader(webstream, Encoding.UTF8)
Dim html As String = streamreader.ReadToEnd
'Now we have the source of the page lets parse and get the challenge url
Dim web As IHTMLDocument2 = New HTMLDocumentClass()
web.write(html)
web.close()
Dim allElements As IHTMLElementCollection = web.body.all
Dim allInputs As IHTMLElementCollection = allElements.tags("script")
Dim element As IHTMLElement
Dim challengeUrl As String = ""
Dim tagName As String = ""
For Each element In allScript
tagName = element.getAttribute("src")
If (Not tagName = Nothing) Then
If (tagName.Contains("http://api.recaptcha.net/")) Then
challengeUrl = tagName
Exit For
End If
End If
Next
'Now we have the challenge url in challengeUrl variable, so lets "GET" it
Dim request1 As HttpWebRequest = HttpWebRequest.Create(challengeUrl)
Dim response1 as HttpWebResponse
response1 = CType(request1.GetResponse, HttpWebResponse)
Dim webstream As Stream = response1.GetResponseStream
Dim streamreader As New StreamReader(webstream, Encoding.UTF8)
Dim temp As String = streamreader.ReadToEnd
'Now parse the index or whatever of that image, it is the thing that we are looking after
Dim startPos As Integer = temp.IndexOf("challenge : '")
startPos += 13
temp = temp.Substring(startPos)
Dim endPos As Integer = temp.IndexOf("'")
temp = (temp.Substring(0, endPos))
'Now the temp variabloe contains the key for the image.. You can download the image from
"http://www.google.com/recaptcha/api/image?c=" & temp
This code is fragmented so may not work by just copying and pasting...
I wrote this while making Pligg submitter bot which isnt complete. You can help me if you can here
* - The-One - Jun 7, 2011
Why not just fetch the url? And load it in a picturebox?
Code Dim str As String = New StreamReader(HttpWebRequest.Create("http://captchas.net/registration/").GetResponse.GetResponseStream).ReadToEnd
PictureBox1.Image = Image.FromStream(HttpWebRequest.Create((GetBetween(str, "id=""captchas.net"" src=""", """ width=""240""")).Replace("amp;", Nothing)).GetResponse.GetResponseStream())
Sorry if I misunderstood you.
* - idb. - Jun 7, 2011
(Jun 7, 2011 05:11 AM)jangedoo Wrote: here is a tutorial for getting reCaptcha image into a picture box.
Ok lets get to the point of getting reCaptcha image.
1. first download the page using httpwebrequest
2. then parse the reCaptcha url
3. goto that challenge url and you'll find the actual "key" for that image
4. parse that key and goto http://www.google.com/recaptcha/api/image?c=PARSED_KEY to get the image
Code Dim request As HttpWebRequest = HttpWebRequest.Create("www.pagewithcaptcha.com")
Dim response as HttpWebResponse
response = CType(request.GetResponse, HttpWebResponse)
Dim webstream As Stream = response.GetResponseStream
Dim streamreader As New StreamReader(webstream, Encoding.UTF8)
Dim html As String = streamreader.ReadToEnd
'Now we have the source of the page lets parse and get the challenge url
Dim web As IHTMLDocument2 = New HTMLDocumentClass()
web.write(html)
web.close()
Dim allElements As IHTMLElementCollection = web.body.all
Dim allInputs As IHTMLElementCollection = allElements.tags("script")
Dim element As IHTMLElement
Dim challengeUrl As String = ""
Dim tagName As String = ""
For Each element In allScript
tagName = element.getAttribute("src")
If (Not tagName = Nothing) Then
If (tagName.Contains("http://api.recaptcha.net/")) Then
challengeUrl = tagName
Exit For
End If
End If
Next
'Now we have the challenge url in challengeUrl variable, so lets "GET" it
Dim request1 As HttpWebRequest = HttpWebRequest.Create(challengeUrl)
Dim response1 as HttpWebResponse
response1 = CType(request1.GetResponse, HttpWebResponse)
Dim webstream As Stream = response1.GetResponseStream
Dim streamreader As New StreamReader(webstream, Encoding.UTF8)
Dim temp As String = streamreader.ReadToEnd
'Now parse the index or whatever of that image, it is the thing that we are looking after
Dim startPos As Integer = temp.IndexOf("challenge : '")
startPos += 13
temp = temp.Substring(startPos)
Dim endPos As Integer = temp.IndexOf("'")
temp = (temp.Substring(0, endPos))
'Now the temp variabloe contains the key for the image.. You can download the image from
"http://www.google.com/recaptcha/api/image?c=" & temp
This code is fragmented so may not work by just copying and pasting...
I wrote this while making Pligg submitter bot which isnt complete. You can help me if you can here
What's with all the extra nonsense? This is like a mix between using WebRequests and the WebBrowser component. Why? Why not just manipulate the Html directly with strings. Why convert it to an IHTMLDocument2? All that is doing is resulting in more work/code. If you have a good explanation as to why you are doing this, that's fine... I'm just curious as to why you'd want to make more work for yourself.
|