Imports System
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Threading
Class DeathByCaptcha
Private username As String = My.Settings.DeathByCaptchaUser
Private password As String = My.Settings.DeathByCaptchaPass
Private CaptchaID As String = ""
Public Function Return_Content(ByVal URL As String) As String
Dim Content As String = Nothing
Dim request As Net.HttpWebRequest = CType(Net.WebRequest.Create(URL), HttpWebRequest)
request.Proxy = Nothing
request.Method = "GET"
request.Timeout = 10000
request.AllowAutoRedirect = True
Dim response As Net.HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
If response.StatusCode = Net.HttpStatusCode.OK Then
Dim responseStream As IO.StreamReader = New IO.StreamReader(response.GetResponseStream())
Content = responseStream.ReadToEnd()
End If
response.Close()
Return Content
End Function
Public Function Post_Content(ByVal URL As String, ByVal Data As String) As String
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(Data)
Dim Content As String = Nothing
' New Request
System.Net.ServicePointManager.Expect100Continue = False
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest)
' Set Proxy Details
request.Proxy = Nothing
' Request Settings
request.Method = "POST"
request.ServicePoint.Expect100Continue = False
request.KeepAlive = False
request.AllowAutoRedirect = False
request.Timeout = 20000
' Headers
request.ContentLength = byteData.Length
request.ContentType = "application/x-www-form-urlencoded"
' Read Stream
Dim postreqstream As Stream = request.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(request.GetResponse(), HttpWebResponse)
' Check HTTP Status
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Content = postreqreader.ReadToEnd
' Close Connection
postresponse.Close()
' Return HTML Content
If Content = Nothing Then Return "" Else Return Content
End Function
Public Function ConvertFileToBase64(ByVal fileName As String) As String
Dim ReturnValue As String = ""
If My.Computer.FileSystem.FileExists(fileName) Then
Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
ReturnValue = Convert.ToBase64String(BinBytes)
BinaryFile.Close()
End Using
End If
Return ReturnValue
End Function
Public Function PostCaptcha(ByVal FilePath As String) As String
Dim captchaResponse As String = ""
Dim boundary As String = "-----------------------------" & DateTime.Now.Ticks.ToString("x")
Dim builder As New StringBuilder()
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("""captchafile""; filename=captcha.jpg")
builder.Append(vbCr & vbLf & vbCr & vbLf)
builder.Append("base64:" & ConvertFileToBase64(FilePath))
builder.Append(vbCr & vbLf)
Dim headerBytes As Byte() = Encoding.ASCII.GetBytes(builder.ToString())
Dim footerBytes As Byte() = Encoding.ASCII.GetBytes((vbCr & vbLf & boundary & "--"))
Dim postBytes As Byte() = New Byte(headerBytes.Length + (footerBytes.Length - 1)) {}
Buffer.BlockCopy(headerBytes, 0, postBytes, 0, headerBytes.Length)
Buffer.BlockCopy(footerBytes, 0, postBytes, headerBytes.Length, footerBytes.Length)
Try
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://api.dbcapi.me/api/captcha"), HttpWebRequest)
' req.Proxy = Nothing
req.Method = "POST"
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = postBytes.Length
req.ServicePoint.Expect100Continue = False
req.AllowAutoRedirect = False
' req.Timeout = 30000
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 TempResponse As String = GetBetween(captchaResponse, "&captcha=", "&")
If TempResponse = Nothing Then Throw New Exception("Could Not Parse Response From DeathByCaptcha")
CaptchaID = TempResponse
' Go Captcha ID Now Go To Status Page
Dim CaptchaAnswer As String = ""
For i = 0 To 10
Dim Content As String = Return_Content("http://api.dbcapi.me/api/captcha/" & CaptchaID)
CaptchaAnswer = GetBetween(Content, "&text=", "&")
If CaptchaAnswer <> Nothing Then
CaptchaAnswer = CaptchaAnswer.Replace("+", " ")
Exit For
Else
Thread.Sleep(2000)
End If
Next
If CaptchaAnswer = Nothing Then Throw New Exception("DeathByCaptcha Exceeded 20 Second Time Limit. Skipping Captcha Request")
Return CaptchaAnswer
Catch ex As Exception
If ex.Message.Contains("403") Then Throw New Exception("Incorrect DeathByCaptcha Login Details")
If ex.Message.Contains("400") Then Throw New Exception("DeathByCaptcha Rejected The Captcha Image")
If ex.Message.Contains("500") Then Throw New Exception("DeathByCaptcha Server Error Occured")
If ex.Message.Contains("503") Then Throw New Exception("DeathByCaptcha Service Is Temporarily Unavailable")
Throw New Exception(ex.Message)
End Try
End Function
Public Sub BadCaptcha()
Post_Content("http://api.dbcapi.me/api/captcha/" & CaptchaID & "/report", "username=" & username & "&password=" & password)
End Sub
End Class