Bài viết chỉ nhằm mục đích học tập, cách gọi API, không sử dụng cho mục đích xấu truy cập website trái phép.
[VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
AZCaptcha là dịch vụ api giải captcha: Image Captcha (normal captcha), Hcaptcha, recaptcha v1, v2 of Google...
Dưới đây, là hình ảnh demo ứng dụng:
Đầu tiên, các bạn cần đăng ký 1 tài khoản API tại đường dẫn: https://azcaptcha.com/
Với tài khoản đăng ký bạn sẽ được request miễn phí 50 lần.
Còn phí thì chỉ có 0/4$ / 1000 captcha, và mua tối thiểu phải 10$ qua cổng thanh toán: paypal, visa...
Đầu tiên, mình đã tạo sẵn 1 class AZCaptchaHelper.vb, để gọi Api này.
Video hướng dẫn Step by step:
Source code AZCaptchaHelper.vb
Imports System.IO Imports System.Net Imports System.Text Public Class AZCaptchaHelper Public Shared Function GetHTMLForm(ByVal Url As String) As String Dim myRequest As HttpWebRequest = CType(WebRequest.Create(Url), HttpWebRequest) myRequest.Method = "GET" Dim myResponse As WebResponse = myRequest.GetResponse() Dim sr As StreamReader = New StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8) Dim result As String = sr.ReadToEnd() sr.Close() myResponse.Close() Return result End Function Public Shared Function ImageToBase64(img As Image) As String Return Convert.ToBase64String(ImageToByteArray(img)) End Function Public Shared Function ImageToByteArray(img As Image) As Byte() Using strm As New MemoryStream img.Save(strm, img.RawFormat) Return strm.GetBuffer() End Using End Function Public Shared Function GetStringFromCaptcha(base64string As String, API_KEY As String) As String Try System.Net.ServicePointManager.Expect100Continue = False Dim request = CType(WebRequest.Create("http://azcaptcha.com/in.php"), HttpWebRequest) Dim postData = "method=base64&key=" & API_KEY & "&body=" + WebUtility.UrlEncode(base64string) Dim data = Encoding.ASCII.GetBytes(postData) request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" request.ContentLength = data.Length Using stream = request.GetRequestStream() stream.Write(data, 0, data.Length) End Using Dim response = CType(request.GetResponse(), HttpWebResponse) Dim responseString As String = New StreamReader(response.GetResponseStream()).ReadToEnd() If responseString.Contains("OK|") Then Dim captcha As String = "" While True captcha = GetHTMLForm("http://azcaptcha.com/res.php?key=" & API_KEY & "&action=get&id=" & responseString.Split("|"c)(1)) If captcha = "CAPCHA_NOT_READY" Then System.Threading.Thread.Sleep(500) Continue While End If If captcha = "_" Then System.Threading.Thread.Sleep(500) Continue While End If If captcha.Contains("OK|") Then captcha = captcha.Replace("OK|", "") End If Return captcha End While End If Return "" Catch e As Exception Dim tt As String = e.Message Return tt End Try End Function End Class
Và cách gọi sử dụng ở form1.vb
Imports System.IO Imports System.Net Imports System.Text Public Class AZCaptchaHelper Public Shared Function GetHTMLForm(ByVal Url As String) As String Dim myRequest As HttpWebRequest = CType(WebRequest.Create(Url), HttpWebRequest) myRequest.Method = "GET" Dim myResponse As WebResponse = myRequest.GetResponse() Dim sr As StreamReader = New StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8) Dim result As String = sr.ReadToEnd() sr.Close() myResponse.Close() Return result End Function Public Shared Function ImageToBase64(img As Image) As String Return Convert.ToBase64String(ImageToByteArray(img)) End Function Public Shared Function ImageToByteArray(img As Image) As Byte() Using strm As New MemoryStream img.Save(strm, img.RawFormat) Return strm.GetBuffer() End Using End Function Public Shared Function GetStringFromCaptcha(base64string As String, API_KEY As String) As String Try System.Net.ServicePointManager.Expect100Continue = False Dim request = CType(WebRequest.Create("http://azcaptcha.com/in.php"), HttpWebRequest) Dim postData = "method=base64&key=" & API_KEY & "&body=" + WebUtility.UrlEncode(base64string) Dim data = Encoding.ASCII.GetBytes(postData) request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" request.ContentLength = data.Length Using stream = request.GetRequestStream() stream.Write(data, 0, data.Length) End Using Dim response = CType(request.GetResponse(), HttpWebResponse) Dim responseString As String = New StreamReader(response.GetResponseStream()).ReadToEnd() If responseString.Contains("OK|") Then Dim captcha As String = "" While True captcha = GetHTMLForm("http://azcaptcha.com/res.php?key=" & API_KEY & "&action=get&id=" & responseString.Split("|"c)(1)) If captcha = "CAPCHA_NOT_READY" Then System.Threading.Thread.Sleep(500) Continue While End If If captcha = "_" Then System.Threading.Thread.Sleep(500) Continue While End If If captcha.Contains("OK|") Then captcha = captcha.Replace("OK|", "") End If Return captcha End While End If Return "" Catch e As Exception Dim tt As String = e.Message Return tt End Try End Function End Class
PASSWORD UNZIP:
HUNG.PRO.VN