
Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Namespace TBNDecaptcher
Class Decaptcher
Public Sub New(ByVal user As String, ByVal pass As String)
username = user
password = pass
End Sub
Private username As String = ""
Private password As String = ""
Private resultCode As String = ""
Private majorId As String = ""
Private minorId As String = ""
Private type As String = ""
Private timeout As String = ""
Public Function PostCaptcha(ByVal captcha As System.Drawing.Image) As String
Dim captchaResponse As String = ""
Dim boundary As String = "myBoundary"
Dim builder As New StringBuilder()
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""function""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("picture2")
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""username""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(username)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""password""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(password)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""pict_to""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("0")
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""pict_type""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("0")
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""pict""; filename=captcha.jpg")
builder.Append(vbCr & vbLf)
builder.Append("Content-Type: image/jpeg")
builder.Append(vbCr & vbLf & vbCr & vbLf)
Dim headerBytes As Byte() = Encoding.ASCII.GetBytes(builder.ToString())
Dim ms As New MemoryStream()
captcha.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imageBytes As Byte() = ms.ToArray()
Dim footerBytes As Byte() = Encoding.ASCII.GetBytes((vbCr & vbLf & "--") + boundary)
Dim postBytes As Byte() = New Byte(headerBytes.Length + imageBytes.Length + (footerBytes.Length - 1)) {}
Buffer.BlockCopy(headerBytes, 0, postBytes, 0, headerBytes.Length)
Buffer.BlockCopy(imageBytes, 0, postBytes, headerBytes.Length, imageBytes.Length)
Buffer.BlockCopy(footerBytes, 0, postBytes, headerBytes.Length + imageBytes.Length, footerBytes.Length)
Try
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://decaptcher.com/poster/"), HttpWebRequest)
req.Method = "POST"
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = postBytes.Length
req.ServicePoint.Expect100Continue = False
Using reqStream As Stream = req.GetRequestStream()
reqStream.Write(postBytes, 0, postBytes.Length)
End Using
Using resp As WebResponse = req.GetResponse()
Using respStream As Stream = resp.GetResponseStream()
Using read As New StreamReader(respStream)
captchaResponse = read.ReadToEnd()
End Using
End Using
End Using
Dim respValues As String() = captchaResponse.Split("|"c)
resultCode = respValues(0)
majorId = respValues(1)
minorId = respValues(2)
type = respValues(3)
timeout = respValues(4)
captchaResponse = respValues(5)
Return captchaResponse
Catch
Return captchaResponse
End Try
End Function
Public Sub BadCaptcha()
Dim boundary As String = "myBoundary"
Dim builder As New StringBuilder()
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""function""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("picture_bad2")
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""username""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(username)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""password""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(password)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""major_id""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(majorId)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""minor_id""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append(minorId)
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary & vbCr & vbLf & "Content-Disposition: form-data; name=")
builder.Append("""submit""")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("Send")
builder.Append(vbCr & vbLf)
builder.Append("--" & boundary)
Dim postString As String = builder.ToString()
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://decaptcher.com/poster/"), HttpWebRequest)
req.Method = "POST"
req.ContentType = "multipart/form-data; boundary=" & boundary
Using reqStream As Stream = req.GetRequestStream()
reqStream.Write(Encoding.ASCII.GetBytes(postString), 0, postString.Length)
End Using
Using resp As WebResponse = req.GetResponse()
Using respStream As Stream = resp.GetResponseStream()
Using read As New StreamReader(respStream)
Dim result As String = read.ReadToEnd()
End Using
End Using
End Using
End Sub
End Class
End Namespace
Public Class Form1
Dim DecaptcherSubmitter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(TextBox1.Text)))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox4.Text = "Waiting for Decaptcher.com"
wait(500)
Dim Captcha As Image = PictureBox1.Image
Dim DecaptcherSubmitter As New TBNDecaptcher.Decaptcher(TextBox2.Text, TextBox3.Text)
Dim CaptchaResult As String = DecaptcherSubmitter.PostCaptcha(Captcha)
TextBox4.Text = CaptchaResult
End Sub
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class