C¨¢ch s? d?ng C# ?? gi?i quy?t c¨¢c th¨¢ch th?c CAPTCHA c?a Cloudflare Turnstile

Anh Tuan
Data Science Expert
17-Sep-2024

?i?u h??ng qua nh?ng ph?c t?p c?a c¨¢c th? th¨¢ch CAPTCHA c¨® th? l¨¤ m?t nhi?m v? kh¨® kh?n, ??c bi?t l¨¤ khi n¨®i ??n Turnstile c?a Cloudflare. L¨¤ m?t nh¨¤ ph¨¢t tri?n d¨¤y d?n kinh nghi?m, t?i ?? g?p ph?i v? s? h? th?ng CAPTCHA trong nh?ng n?m qua, nh?ng Cloudflare Turnstile ??t ra m?t th¨¢ch th?c ??c ?¨¢o do c¨¢c thu?t to¨¢n tinh vi c?a n¨® ???c thi?t k? ?? ng?n ch?n c¨¢c h? th?ng t? ??ng. Trong h??ng d?n n¨¤y, t?i s? h??ng d?n b?n c¨¢ch x? l? c¨¢c th? th¨¢ch Cloudflare Turnstile CAPTCHA b?ng c¨¢ch s? d?ng C#, cung c?p cho b?n nh?ng hi?u bi?t th?c t? v¨¤ k? thu?t ?? n?ng cao n? l?c t? ??ng h¨®a c?a b?n.
M?c l?c
- Gi?i thi?u v? Cloudflare Turnstile
- Thi?t l?p m?i tr??ng ph¨¢t tri?n C#
- T?i xu?ng v¨¤ c¨¤i ??t .NET
- C?u h¨¬nh VS Code ?? ph¨¢t tri?n C#
- Thu th?p ?i?u ki?n ti¨ºn quy?t s? d?ng API
- ??ng k? CapSolver
- Truy xu?t SiteKey cho Turnstile
- S? d?ng CapSolver API ?? l?y Token Turnstile
- V¨ª d? m? ??y ??
- X? l? l?i v¨¤ kh?c ph?c s? c?
- L?i y¨ºu c?u kh?ng th¨¤nh c?ng
- Gi?i th¨ªch m?
- K?t lu?n
Hi?u v? Cloudflare Turnstile
Cloudflare Turnstile l¨¤ m?t h? th?ng CAPTCHA n?ng cao ???c thi?t k? ?? b?o v? c¨¢c trang web kh?i c¨¢c bot t? ??ng ??ng th?i ??m b?o ¨ªt c?n tr? nh?t ??i v?i ng??i d¨´ng h?p ph¨¢p. Kh?ng gi?ng nh? CAPTCHA truy?n th?ng, th??ng y¨ºu c?u gi?i c?u ?? ho?c x¨¢c ??nh ??i t??ng, Turnstile ho?t ??ng th?ng qua m?t c¨¢ch ti?p c?n tinh t? h?n. N¨® ph?n t¨ªch h¨¤nh vi c?a ng??i d¨´ng v¨¤ c¨¢c t??ng t¨¢c web kh¨¢c nhau ?? x¨¢c ??nh xem kh¨¢ch truy c?p l¨¤ ng??i hay bot.
Turnstile s? d?ng m?t lo?t c¨¢c t¨ªn hi?u, bao g?m chuy?n ??ng chu?t, m? h¨¬nh nh?p chu?t v¨¤ th?i gian t??ng t¨¢c, ?? t?o ra m?t th? th¨¢ch kh¨® ??i v?i c¨¢c h? th?ng t? ??ng. ?i?u n¨¤y l¨¤m cho n¨® tr? th¨¤nh m?t c?ng c? m?nh m? cho b?o m?t trang web nh?ng c?ng l¨¤ m?t tr? ng?i kh¨® kh?n ??i v?i t? ??ng h¨®a.
M? b? sung
Nh?n M? th??ng c?a b?n cho c¨¢c gi?i ph¨¢p CAPTCHA h¨¤ng ??u; CapSolver: WEBS. Sau khi ??i m?, b?n s? nh?n ???c th¨ºm 5% ti?n th??ng sau m?i l?n n?p ti?n, kh?ng gi?i h?n
Thi?t l?p m?i tr??ng ph¨¢t tri?n C#
1. T?i xu?ng v¨¤ c¨¤i ??t .NET
- Truy c?p trang n¨¤y ?? t?i xu?ng .NET.
- L¨¤m theo h??ng d?n ???c cung c?p cho h? ?i?u h¨¤nh c?a b?n ?? c¨¤i ??t .NET.
2. C?u h¨¬nh VS Code ?? ph¨¢t tri?n C#
- C¨¤i ??t ph?n m? r?ng C# cho VS Code.
- Trong VS Code, t¨¬m ki?m "C#" trong th? tr??ng ph?n m? r?ng v¨¤ c¨¤i ??t plugin ch¨ªnh th?c c?a Microsoft.
- Ph?n m? r?ng n¨¤y cung c?p c¨¢c t¨ªnh n?ng nh? IntelliSense v¨¤ ??nh d?ng m?, gi¨²p ph¨¢t tri?n C# d? d¨¤ng h?n.

- C¨¤i ??t g¨®i ph?n t¨ªch JSON
Newtonsoft.Json
?? x? l? d? li?u JSON.- B?n c¨® th? c¨¤i ??t g¨®i n¨¤y b?ng c¨¢ch s? d?ng NuGet v?i l?nh:bash
dotnet add package Newtonsoft.Json
- B?n c¨® th? c¨¤i ??t g¨®i n¨¤y b?ng c¨¢ch s? d?ng NuGet v?i l?nh:
Thu th?p ?i?u ki?n ti¨ºn quy?t s? d?ng API
1. ??ng k? CapSolver
- T?o t¨¤i kho?n tr¨ºn CapSolver ?? truy c?p c¨¢c d?ch v? API c?a h?.
- Sau khi ??ng k?, b?n s? nh?n ???c m?t kh¨®a API c?n thi?t ?? truy c?p c¨¢c d?ch v? gi?i quy?t CAPTCHA c?a CapSolver.
2. Truy xu?t SiteKey cho Turnstile
- ??i v?i c¨¢c th? th¨¢ch Cloudflare Turnstile CAPTCHA, vi?c l?y
siteKey
cho trang web ?¨ªch l¨¤ ?i?u c?n thi?t.siteKey
l¨¤ y¨ºu c?u ?? s? d?ng API gi?i m? v¨¤ gi?i quy?t CAPTCHA. - B?n c¨® th? tr¨ªch xu?t
siteKey
b?ng c¨¢ch s? d?ng Ph?n m? r?ng CapSolver, gi¨²p ??n gi?n h¨®a qu¨¢ tr¨¬nh.
S? d?ng CapSolver API ?? l?y Token Turnstile
??y l¨¤ m? ?? t??ng t¨¢c v?i CapSolver API, y¨ºu c?u gi?i quy?t CAPTCHA v¨¤ l?y token Turnstile.
csharp
public static async Task<string> CallCapsolver()
{
// // G?i y¨ºu c?u GET
// var todoItem = await GetTodoItemAsync(API_URL);
// Console.WriteLine("K?t qu? y¨ºu c?u GET:");
// Console.WriteLine(todoItem);
var data = new
{
clientKey = CAPSOLVER_API_KEY,
task = new
{
type = "AntiTurnstileTaskProxyLess",
websiteURL = PAGE_URL,
websiteKey = SITE_KEY,
metadata = new { action = "login" }
}
};
// G?i y¨ºu c?u POST
var response = await PostTodoItemAsync("https://api.capsolver.com/createTask", data);
Console.WriteLine("K?t qu? y¨ºu c?u POST:");
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
JObject taskResp = JsonConvert.DeserializeObject<JObject>(responseString);
var taskId = taskResp["taskId"].ToString();
if (string.IsNullOrEmpty(taskId))
{
Console.WriteLine("Kh?ng nh?n ???c ID t¨¢c v?.");
return "";
}
Console.WriteLine($"ID t¨¢c v? ?? t?o: {taskId}");
while (true)
{
await Task.Delay(1000); // Ng? trong 1 gi?y
var resultData = new
{
clientKey = CAPSOLVER_API_KEY,
taskId = taskId
};
// content = new StringContent(JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8, "application/json");
// response = await httpClient.PostAsync(uri, content);
response = await PostTodoItemAsync("https://api.capsolver.com/getTaskResult", resultData);
responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Kh?ng th? l?y k?t qu? t¨¢c v?: {responseString}");
return "";
}
taskResp = JsonConvert.DeserializeObject<JObject>(responseString);
Console.WriteLine(taskResp);
var status = taskResp["status"].ToString();
if (status == "ready")
{
Console.WriteLine("Th¨¤nh c?ng => " + responseString);
return taskResp["solution"]["token"].ToString();
}
if (status == "failed")
{
Console.WriteLine("Th?t b?i! => " + responseString);
return "";
}
}
}
V¨ª d? m? ??y ??
csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace HttpExample
{
public class Program
{
private const string CAPSOLVER_API_KEY = "CAI-xxxxxxxxxxxxxxxxxxx";
private const string PAGE_URL = "https://dash.cloudflare.com/login";
private const string SITE_KEY = "0x4AAAAAAAJel0iaAR3mgkjp";
public static async Task Main(string[] args)
{
var token = await CallCapsolver();
Console.WriteLine($"token: {token}");
await Login(token);
}
public static async Task<string> CallCapsolver()
{
// // G?i y¨ºu c?u GET
// var todoItem = await GetTodoItemAsync(API_URL);
// Console.WriteLine("K?t qu? y¨ºu c?u GET:");
// Console.WriteLine(todoItem);
var data = new
{
clientKey = CAPSOLVER_API_KEY,
task = new
{
type = "AntiTurnstileTaskProxyLess",
websiteURL = PAGE_URL,
websiteKey = SITE_KEY,
metadata = new { action = "login" }
}
};
// G?i y¨ºu c?u POST
var response = await PostTodoItemAsync("https://api.capsolver.com/createTask", data);
Console.WriteLine("K?t qu? y¨ºu c?u POST:");
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
JObject taskResp = JsonConvert.DeserializeObject<JObject>(responseString);
var taskId = taskResp["taskId"].ToString();
if (string.IsNullOrEmpty(taskId))
{
Console.WriteLine("Kh?ng nh?n ???c ID t¨¢c v?.");
return "";
}
Console.WriteLine($"ID t¨¢c v? ?? t?o: {taskId}");
while (true)
{
await Task.Delay(1000); // Ng? trong 1 gi?y
var resultData = new
{
clientKey = CAPSOLVER_API_KEY,
taskId = taskId
};
// content = new StringContent(JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8, "application/json");
// response = await httpClient.PostAsync(uri, content);
response = await PostTodoItemAsync("https://api.capsolver.com/getTaskResult", resultData);
responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"Kh?ng th? l?y k?t qu? t¨¢c v?: {responseString}");
return "";
}
taskResp = JsonConvert.DeserializeObject<JObject>(responseString);
Console.WriteLine(taskResp);
var status = taskResp["status"].ToString();
if (status == "ready")
{
Console.WriteLine("Th¨¤nh c?ng => " + responseString);
return taskResp["solution"]["token"].ToString();
}
if (status == "failed")
{
Console.WriteLine("Th?t b?i! => " + responseString);
return "";
}
}
}
public static async Task Login(string token)
{
using var httpClient = new HttpClient();
// Th¨ºm ti¨ºu ?? y¨ºu c?u
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Cookie", $"cf_clearance={token}");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Host", "dash.cloudflare.com");
var data = new {
cf_challenge_response = token,
email = "[email protected]",
password = "123456",
};
var json = JsonConvert.SerializeObject(data);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://dash.cloudflare.com/api/v4/login", content);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Y¨ºu c?u kh?ng th¨¤nh c?ng v?i m? tr?ng th¨¢i {response.StatusCode}");
}
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
private static async Task<HttpResponseMessage> GetTodoItemAsync(string url)
{
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Y¨ºu c?u kh?ng th¨¤nh c?ng v?i m? tr?ng th¨¢i {response.StatusCode}");
}
// var responseString = await response.Content.ReadAsStringAsync();
return response;
}
private static async Task<HttpResponseMessage> PostTodoItemAsync(string url, object item)
{
using var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(item);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Y¨ºu c?u kh?ng th¨¤nh c?ng v?i m? tr?ng th¨¢i {response.StatusCode}");
}
return response;
}
}
}
X? l? l?i v¨¤ kh?c ph?c s? c?
- Y¨ºu c?u kh?ng th¨¤nh c?ng: N?u b?n g?p l?i "Y¨ºu c?u kh?ng th¨¤nh c?ng", h?y x¨¢c minh xem kh¨®a API v¨¤ kh¨®a trang web c¨® ch¨ªnh x¨¢c hay kh?ng.
- ??m b?o b?n c¨® kh¨®a API ho?t ??ng t? t¨¤i kho?n CapSolver c?a m¨¬nh.
- Ki?m tra l?i xem
siteKey
c¨® kh?p v?isiteKey
t? trang web ?¨ªch hay kh?ng.
Gi?i th¨ªch
- Thi?t l?p t¨¢c v?: Trong ph??ng th?c
CallCapsolver
, b?n x¨¢c ??nh ki?u t¨¢c v?AntiTurnstileTaskProxyLess
,websiteURL
v¨¤websiteKey
. C¨¢c tham s? n¨¤y ???c g?i ??n CapSolver ?? t?o t¨¢c v? gi?i quy?t CAPTCHA. - L?y th?ng tin tr?ng th¨¢i t¨¢c v?: Sau khi t¨¢c v? ???c t?o, m? s? l?y th?ng tin t? ?i?m cu?i
getTaskResult
?? ki?m tra tr?ng th¨¢i c?a t¨¢c v?. N?u t¨¢c v? ?? s?n s¨¤ng, n¨® s? l?y gi?i ph¨¢p (token Turnstile); n?u n¨® th?t b?i, n¨® s? tr? v? l?i. - S? d?ng token: Ph??ng th?c
Login
s? d?ng token nh?n ???c t? CapSolver ?? x¨¢c th?c y¨ºu c?u ??ng nh?p tr¨ºn trang web ???c b?o v? b?i Cloudflare.
K?t lu?n
B?ng c¨¢ch l¨¤m theo h??ng d?n n¨¤y, b?n s? c¨® th? ?i?u h??ng qua nh?ng ph?c t?p c?a vi?c gi?i quy?t c¨¢c th? th¨¢ch Cloudflare Turnstile CAPTCHA b?ng c¨¢ch s? d?ng C#. API c?a CapSolver cung c?p m?t c¨¢ch ?¨¢ng tin c?y v¨¤ hi?u qu? ?? t? ??ng h¨®a quy tr¨¬nh, c?i thi?n kh? n?ng t? ??ng h¨®a c?a b?n. ?? bi?t th¨ºm th?ng tin v¨¤ c?p nh?t, h?y truy c?p CapSolver.
L?u ? v? tu?n th?
Quan tr?ng: Khi tham gia v¨¤o web scraping, ?i?u quan tr?ng l¨¤ ph?i tu?n th? c¨¢c h??ng d?n ph¨¢p l? v¨¤ ??o ??c. Lu?n ??m b?o r?ng b?n c¨® quy?n scraping trang web ?¨ªch v¨¤ t?n tr?ng t?p
robots.txt
v¨¤ ?i?u kho?n d?ch v? c?a trang web. CapSolver ph?n ??i vi?c s? d?ng sai d?ch v? c?a ch¨²ng t?i cho b?t k? ho?t ??ng n¨¤o kh?ng tu?n th?. S? d?ng sai c¨¢c c?ng c? t? ??ng ?? b? qua CAPTCHA m¨¤ kh?ng c¨® quy?n h?p l? c¨® th? d?n ??n h?u qu? ph¨¢p l?. H?y ch?c ch?n r?ng ho?t ??ng scraping c?a b?n tu?n th? t?t c? c¨¢c lu?t v¨¤ quy ??nh hi?n h¨¤nh ?? tr¨¢nh c¨¢c v?n ?? ti?m ?n.
Tuy¨ºn b? Tu?n th?: Th?ng tin ???c cung c?p tr¨ºn blog n¨¤y ch? mang t¨ªnh ch?t tham kh?o. CapSolver cam k?t tu?n th? t?t c? c¨¢c lu?t v¨¤ quy ??nh hi?n h¨¤nh. Vi?c s? d?ng m?ng l??i CapSolver cho c¨¢c ho?t ??ng b?t h?p ph¨¢p, gian l?n ho?c l?m d?ng l¨¤ ho¨¤n to¨¤n b? c?m v¨¤ s? b? ?i?u tra. C¨¢c gi?i ph¨¢p gi?i captcha c?a ch¨²ng t?i n?ng cao tr?i nghi?m ng??i d¨´ng trong khi ??m b?o tu?n th? 100% trong vi?c gi¨²p gi?i quy?t c¨¢c kh¨® kh?n v? captcha trong qu¨¢ tr¨¬nh thu th?p d? li?u c?ng khai. Ch¨²ng t?i khuy?n kh¨ªch vi?c s? d?ng d?ch v? c?a ch¨²ng t?i m?t c¨¢ch c¨® tr¨¢ch nhi?m. ?? bi?t th¨ºm th?ng tin, vui l¨°ng truy c?p ?i?u kho?n D?ch v? v¨¤ Ch¨ªnh s¨¢ch Quy?n ri¨ºng t?.
Th¨ºm

C¨¢ch gi?i quy?t th? th¨¢ch Cloudflare
V??t qua th? th¨¢ch Cloudflare d? d¨¤ng v?i CapSolver. H??ng d?n n¨¤y bao g?m nh?ng ?i?u c?n thi?t ?? gi?i quy?t b?o m?t c?a Cloudflare, ??m b?o ?i?u h??ng th¨¤nh c?ng v?i c¨¢c b??c v¨¤ l?i khuy¨ºn chi ti?t.

Anh Tuan
23-Sep-2025

C¨¢ch gi?i quy?t th? th¨¢ch Cloudflare JS ?? thu th?p d? li?u web v¨¤ t? ??ng h¨®a
T¨¬m hi?u c¨¢ch gi?i quy?t th? th¨¢ch JavaScript c?a Cloudflare ?? thu th?p d? li?u web v¨¤ t? ??ng h¨®a li?n m?ch. Kh¨¢m ph¨¢ c¨¢c chi?n l??c hi?u qu?, bao g?m s? d?ng tr¨¬nh duy?t kh?ng ??u, lu?n phi¨ºn proxy, v¨¤ t?n d?ng kh? n?ng gi?i quy?t CAPTCHA n?ng cao c?a CapSolver.

Anh Tuan
05-Mar-2025

D?u v?n tay TLS c?a Cloudflare: N¨® l¨¤ g¨¬ v¨¤ c¨¢ch gi?i quy?t
T¨¬m hi?u v? vi?c Cloudflare s? d?ng d?u v?n tay TLS ?? b?o m?t, c¨¢ch n¨® ph¨¢t hi?n v¨¤ ch?n bot, v¨¤ kh¨¢m ph¨¢ c¨¢c ph??ng ph¨¢p hi?u qu? ?? gi?i quy?t v?n ?? n¨¤y cho c¨¢c t¨¢c v? thu th?p d? li?u web v¨¤ duy?t web t? ??ng.

Anh Tuan
28-Feb-2025

C¨¢ch tr¨ªch xu?t d? li?u t? trang web ???c b?o v? b?i Cloudflare
Trong h??ng d?n n¨¤y, ch¨²ng ta s? kh¨¢m ph¨¢ c¨¢c k? thu?t ??o ??c v¨¤ hi?u qu? ?? tr¨ªch xu?t d? li?u t? c¨¢c trang web ???c b?o v? b?i Cloudflare.

Anh Tuan
20-Feb-2025

C¨¢ch gi?i quy?t Cloudflare b?ng Python v¨¤ Go v¨¤o n?m 2025
S? chia s? nh?ng hi?u bi?t v? Cloudflare Turnstile, s? d?ng Python v¨¤ Go cho nh?ng t¨¢c v? n¨¤y, li?u Turnstile c¨® th? ph¨¢t hi?n scraper Python, v¨¤ c¨¢ch v??t qua n¨® m?t c¨¢ch hi?u qu? b?ng c¨¢c gi?i ph¨¢p nh? CapSolver.

Anh Tuan
05-Nov-2024

C¨¢ch gi?i quy?t Captcha Turnstile c?a Cloudflare b?ng Selenium
Trong blog n¨¤y, ch¨²ng ta s? th?o lu?n v? m?t s? k? thu?t hi?u qu? ?? v??t qua Cloudflare Turnstile Captchas b?ng Selenium.

Anh Tuan
11-Oct-2024