Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Namespace TBNDecaptcher
Class Decaptcher
Private username As String = My.Settings.DecaptcherUser
Private password As String = My.Settings.DecaptcherPass
Private resultCode As String = ""
Private majorId As String = ""
Private minorId As String = ""
Private type As String = ""
Private timeout As String = ""
Public Function PostCaptcha(ByVal FilePath As String) As String
Dim captcha As System.Drawing.Image
Dim fs As New System.IO.FileStream(FilePath, System.IO.FileMode.Open)
captcha = Image.FromStream(fs)
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)
fs.Close()
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://poster.decaptcher.com/"), 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