Captcha with Webrequest

Marcoluca56

Newbie
Joined
Apr 1, 2014
Messages
24
Reaction score
0
Hi guys!
I am looking for a way that allow me to show captcha in a picturebox or in other way, with Webrequest, so WITHOUT Webbrowser.
This is my code:


Code:
Dim logincookie As CookieContainer


 Dim postData As String = "......"
      Dim tempCookies As New CookieContainer
      Dim encoding As New UTF8Encoding
      Dim byteData As Byte() = encoding.GetBytes(postData)


      Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("....."), HttpWebRequest)
      postReq.Method = "POST"
      postReq.KeepAlive = True
      postReq.CookieContainer = tempCookies
      postReq.ContentType = "application/x-www-form-urlencoded"
      postReq.Referer = "....."
      postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
      postReq.ContentLength = byteData.Length


      Dim postreqstream As Stream = postReq.GetRequestStream()
      postreqstream.Write(byteData, 0, byteData.Length)
      postreqstream.Close()
      Dim postresponse As HttpWebResponse


      postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
      tempCookies.Add(postresponse.Cookies)
      logincookie = tempCookies


Thanks in advance ^_^
 
I have used this simple snippet in the past:

Code:
Dim client As WebClient = New WebClient()

Dim thePic As Byte() = client.DownloadData("http://192.168.1.110/Captcha.jpg")

Dim stream As System.IO.MemoryStream
Dim img As Image

stream = New System.IO.MemoryStream(thePic)
img = Image.FromStream(stream)
PictureBox1.Image = img

Hope it helps!
 
Thanks, but this does not works with captcha that change every time.
For example with this: registraXmailXvirgilioXit (replace X with ".")

Can you help me, please?
 
Yup ^ Don't even need the img variable.

Code:
Dim dataStream As Stream
                Dim response As HttpWebResponse = request.GetResponse()
                dataStream = response.GetResponseStream()
                Picturebox1.Image = Image.FromStream(response.GetResponseStream())
 
Thanks, but this does not works with captcha that change every time.
For example with this: registraXmailXvirgilioXit (replace X with ".")

Can you help me, please?

You need to parse the link from this line:

Code:
<img id="captcha" class="block" alt="captcha" src="https://registra.mail.virgilio.it:443/vmail/captcha/12f67bc7de6d1a6e33aa14e45f02ffd/captcha.jpg"></img>

Then make a request to the image and use the what we said before.
 
I have tried with this code, but does not works :(
Code:
 Dim postData As String = "...."
        
Dim tempCookies As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)


        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("......"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookies
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "......."
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
        postReq.ContentLength = byteData.Length


        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        Dim postresponse As HttpWebResponse


        Dim dataStream As Stream
        Dim response As HttpWebResponse = postReq.GetResponse()
        dataStream = response.GetResponseStream()
        PictureBox1.Image = Image.FromStream(response.GetResponseStream())


        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
        tempCookies.Add(postresponse.Cookies)
        logincookie = tempCookies
 
As I told you already.. First you request the login page. Then you parse out the captcha img src and request the image.


I don't know how can i "parse out the captcha img src and request the image..."
 
I don't know how can i "parse out the captcha img src and request the image..."

You get the html from the login page request with this at the bottom of your request

Code:
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()

Parsing text is a pretty simple concept. Google it. You parse the img out of the response and make a new request to the parsed img link.

It looks like your understanding of vb.net and httpwebrequests is very low and there isn't much more I can do to help you than spoon feeding, which I'm not willing to do.
 
I'm trying, but I can not ... will not charge the captcha: (

pastebinXcom/ZU8jsKiy

(replace X with ".")
 
Code:
            Dim CaptchaImage As String

            Dim request As HttpWebRequest = HttpWebRequest.Create("https://registra.mail.virgilio.it/vmail/vmail.do")
            request.CookieContainer = New CookieContainer
            'request.Proxy = myProxy
            request.Timeout = 10000
            request.ReadWriteTimeout = 10000
            Dim response As HttpWebResponse = request.GetResponse()
            Dim dataStream As Stream
            dataStream = response.GetResponseStream()
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            response.Close()
            dataStream.Close()

            CaptchaImage = GetBetween(responseFromServer, "<img src=""https://registra.mail.virgilio.it:443/vmail/captcha/", """")

            Debug.Print("https://registra.mail.virgilio.it:443/vmail/captcha/" & CaptchaImage)

            'SIGNUP
            request = HttpWebRequest.Create("https://registra.mail.virgilio.it:443/vmail/captcha/" & CaptchaImage)
            request.CookieContainer = New CookieContainer
            request.Timeout = 10000
            request.ReadWriteTimeout = 10000
            Dim dataStream3 As Stream
            Dim response3 As HttpWebResponse = request.GetResponse()
            dataStream3 = response3.GetResponseStream()
            PictureBox1.Image = Image.FromStream(response3.GetResponseStream())


            Dim reader3 As New StreamReader(dataStream3)
            responseFromServer = reader3.ReadToEnd()
            reader3.Close()
            response3.Close()
            dataStream3.Close()

        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

    Private Function GetBetween(ByVal InputText As String, _
                                 ByVal starttext As String, _
                                 ByVal endtext As String)

        Dim startPos As Integer
        Dim endPos As Integer
        Dim lenStart As Integer
        startPos = InputText.IndexOf(starttext, StringComparison.CurrentCultureIgnoreCase)
        If startPos >= 0 Then
            lenStart = startPos + starttext.Length
            endPos = InputText.IndexOf(endtext, lenStart, StringComparison.CurrentCultureIgnoreCase)
            If endPos >= 0 Then
                Return InputText.Substring(lenStart, endPos - lenStart)
            End If
        End If
        Return "ERROR"
    End Function
 
Thanks gtothem, but your code does not works, because Visual Studio when i run it, give me this error: "Remote server error: (503) Server Unavailable." Why?
 
Did you get this yet?

After you load your mail URL, then you can parse out the CAPTCHA URL by using this regex pattern:
Code:
Dim Rx As New Regex("img src=""(http[\w\W]+?captcha.jpg)""")
Dim CaptchURL As String = Rx.Match(YOURHTML).Groups(1).Value

Now you can download the image from CaptchaURL.

If the HTML changes, you need to change the Regex pattern as well.

Don't forgets:
To add
Code:
Imports System.Text.RegularExpressions
on top of your code.
To send cookies along with this request.
 
No son, not going to work. If you are going to HTTPWebRequest then you need to parse the HTML page yourself, that includes finding the captcha image. When it is found, you can load the image, but before that, nothing.
 
Back
Top