# hCAPTCHA

hCaptcha là một loại captcha khá mới thực sự giống với reCAPTCHA và trông giống như sau:<br>

![](/files/LyTfu5I5NZ1ownLrlHEi) ![](/files/6DpGo4kjLOD8Eh8Mhdx2)

### 1. Tìm data-sitekey của hCAPTCHA

Đầu tiên, bạn cần tìm giá trị của tham số <mark style="color:red;">`data-sitekey`</mark> trong mã nguồn của trang web chứa hCAPTCHA. Mở bảng điều khiển dành cho nhà phát triển trong trình duyệt của bạn và tìm phần tử có thuộc tính <mark style="color:red;">`data-sitekey`</mark>

```
<div class="h-captcha" data-sitekey="f7de0da3-3303-44e8-ab48-fa32ff8ccc7b" id="hcaptcha"></div>
```

<figure><img src="/files/bhctmeLdyxBbsNY1TiYU" alt=""><figcaption></figcaption></figure>

### 2. Tạo yêu cầu/Request

<mark style="color:red;">**POST :**</mark> <mark style="color:red;"></mark><mark style="color:red;">`https://anticaptcha.top/api/captcha`</mark>

**Các tham số Body**:

<table data-header-hidden><thead><tr><th width="157">Tên trường dữ liệu</th><th width="131"></th><th width="105"></th><th></th></tr></thead><tbody><tr><td><strong>Tên trường dữ liệu</strong></td><td><strong>Kiểu dữ liệu</strong></td><td><strong>Bắt buộc</strong></td><td><strong>Mô tả</strong></td></tr><tr><td>apikey</td><td>String</td><td>x</td><td>Key duy nhất để xác định đối tác API</td></tr><tr><td>type</td><td>Interger</td><td>x</td><td>Giá trị: <mark style="color:red;"><strong>15</strong></mark></td></tr><tr><td>websitekey</td><td>String</td><td>x</td><td><p>Data-</p><p>sitekey hCaptcha cần view code url chứa hCaptcha để lấy sitekey</p></td></tr><tr><td>pageurl</td><td>String</td><td>x</td><td>url của webpage hiển thị hCaptcha mà bạn muốn giải<br>Ví dụ: <a href="https://accounts.hcaptcha.com/demo">https://accounts.hcaptcha.com/demo</a></td></tr></tbody></table>

```http
POST HTTP
Url: https://anticaptcha.top/api/captcha
Content-Type: application/json

{
	"apikey": "YOUR_API_KEY",
	"type": 15, // id cho hCAPTCHA
	"websitekey": "a5f74b19-9e45-40e0-b45d-47ff91b7a6c2", // thay bằng sitekey của bạn
	"pageurl": "https://accounts.hcaptcha.com/demo" // thay bằng url của bạn
}
```

### 3. Nhận kết quả trả về/Response

Kết quả trả về dạng JSON gồm các trường sau

<table data-header-hidden><thead><tr><th width="201"></th><th width="137"></th><th></th></tr></thead><tbody><tr><td><strong>Tên trường dữ liệu</strong></td><td><strong>Kiểu dữ liệu</strong></td><td><strong>Mô tả</strong></td></tr><tr><td>success</td><td>boolean</td><td><p>Mã thông báo xác định kết quả của bước gửi captcha qua POST</p><p><mark style="color:red;"><strong>true</strong></mark>: Thành công<br><mark style="color:red;"><strong>false</strong></mark>: Thất bại</p></td></tr><tr><td>message</td><td>String</td><td>Thông báo tương ứng nếu có</td></tr><tr><td>captcha</td><td>String</td><td>kết quả giải ra = token dùng để vượt hCAPTCHA</td></tr><tr><td>base64img</td><td>String</td><td>Không dùng đến</td></tr></tbody></table>

{% tabs %}
{% tab title="Thành công" %}

```json
{
	"success": true,
	"message": "Thành công",
	"captcha": "03AFY_a8V9QvDKlP0CzK-3BKT5YZ9Eam3QRWT5m4-oPWygX7fhA.....", //kết quả giải ra
	"base64img": null
}
```

{% endtab %}

{% tab title="Thất bại" %}

```json
{
	"success": false,
	"message": null,
	"captcha": null,
	"base64img": null
}
```

hoặc

```json
{
	"success": false,
	"message": "Số xu không đủ",
	"captcha": ""
}
```

hoặc

```json
{
	"success": false,
	"message": "Api key không chính xác",
	"captcha": ""
}
```

{% endtab %}
{% endtabs %}

### 4. Code mẫu

{% tabs %}
{% tab title="C#" %}

```csharp
public class CatpchaResult
{
    public string log { get; set; }
    public bool status { get; set; }
    public string catcha { get; set; }
}
public class AnticaptchaTopApi
{
    public string apiKey = "";
    public string Post(string url, object data, string method = "POST")
    {
        int num = 0;
        NameValueCollection values;
        while (num < 1)
        {
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    values = new NameValueCollection();
                    if (data != null)
                    {
                        data.GetType().GetProperties().ToList().ForEach(delegate (PropertyInfo pi)
                        {
                            values.Add(pi.Name, (pi.GetValue(data, null) ?? "").ToString());
                        });
                        byte[] bytes = webClient.UploadValues(url, method, values);
                        return Encoding.UTF8.GetString(bytes);
                    }
                    return webClient.DownloadString(url);
                }
             }
            catch
            {                
            }
        }
        return "";
    }
    public CatpchaResult GetCatpcha(int type=15,string websitekey,string pageurl)
    {
        var data = new
        {
            apikey = apiKey,
            type = type,
            websitekey = websitekey,
            pageurl = pageurl
        };
        var rs = new CatpchaResult
        {
            status = false
        };
        try
        {
            var result = JsonConvert.DeserializeObject<dynamic>(Post("https://anticaptcha.top/api/captcha", data));
            if (result.success == true)
            {
                rs.status = true;
                rs.catcha = result.captcha;
                rs.log = result.message;
            }
        }
        catch
        {
        }
        return rs;
    }
}
```

{% endtab %}

{% tab title="PHP Curl" %}

```php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://anticaptcha.top/api/captcha",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"apikey\"\r\n\r\n[apikey]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\n15\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"websitekey\"\r\n\r\na5f74b19-9e45-40e0-b45d-47ff91b7a6c2\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"pageurl\"\r\n\r\n[url chứa hcaptcha]\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
```

{% endtab %}

{% tab title="Javascript Jquery AJAX" %}

```javascript
var form = new FormData();
form.append("apikey", "[apikey]");
form.append("type", "15");
form.append("websitekey", "a5f74b19-9e45-40e0-b45d-47ff91b7a6c2");
form.append("pageurl", "[url chứa hcaptcha]");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://anticaptcha.top/api/captcha",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
```

{% endtab %}
{% endtabs %}

### 5. Sử dụng kết quả token nhận được

#### 5.1. Vượt hCAPTCHA dạng click

![](/files/LyTfu5I5NZ1ownLrlHEi)

Định vị phần tử có id h-captcha-response và g-recaptcha-response và làm cho nó hiển thị bằng cách xóa tham số display:none.

<figure><img src="/files/lRR4QQbtkj800WWed38z" alt=""><figcaption></figcaption></figure>

{% hint style="info" %} <mark style="color:red;">**Xin lưu ý**</mark>: đôi khi nội dung trên trang được tạo động và bạn sẽ không thấy phần tử này trong mã nguồn html. Trong những trường hợp như vậy, bạn phải khám phá mã javascript mà tạo ra nội dung. Tùy chọn "Inspect" (F12) trong Google Chrome có thể giúp ích trong việc đó.
{% endhint %}

Như một sự thay thế bạn chỉ có thể sử dụng javascript để đặt giá trị của trường h-captcha-response và g-recaptcha-response:

```
document.getElementById("g-recaptcha-response").innerHTML="TOKEN_FROM_ANTICAPTCHA";
```

Một trường input textarea sẽ xuất hiện. Và bạn chỉ phải paste giá trị token nhận được vào đó và submit form.

![](/files/8a0aMcwjvBZssSPgODFi)

Xin chúc mừng, bạn đã vượt qua hCAPTCHA\
![](/files/RUCpXhYRlPeTfHg21mZ3)

#### 5.2. Vượt hCAPTCHA dạng callback

Đôi khi không có nút submit và thay vào đó một hàm callback được sử dụng. Hàm này được thực thi khi hCAPTCHA được giải quyết.\
Hàm callback thường được xác định trong tham số `data-callback` của hCAPTCHA, ví dụ:

```
data-callback="myCallbackFunction"
```

Hoặc thỉnh thoảng nó được xác định là tham số `callback` của hàm `grecaptcha.render`, ví dụ:

```
grecaptcha.render('example', {
  'sitekey' : 'someSitekey',
  'callback' : myCallbackFunction,
  'theme' : 'dark'
});
```

Cũng có một cách khác để tìm hàm callback - mở javascript console của trình duyệt của bạn và khám phá đối tượng cấu hình hCAPTCHA:

```
___grecaptcha_cfg.clients[0].aa.l.callback
```

Chú ý là **aa.l** có thể thay đổi và có thể có nhiều clients vì vậy bạn cũng phải kiểm tra **clients\[1], clients\[2]**.

Cuối cùng, tất cả những gì bạn phải làm là gọi hàm đó:

```
myCallbackFunction();
```

Hoặc thậm chí theo cách này:

```
___grecaptcha_cfg.clients[0].aa.l.callback();
```

Đôi khi nó được yêu cầu cung cấp một đối số và trong hầu hết các trường hợp, bạn nên đặt mã thông báo ở đó. Ví dụ:

```
myCallbackFunction('TOKEN');
```

**Làm cách nào vượt invisible reCAPTCHA trong trình duyệt?**

**Phương thức 1: sử dụng javascript:**

1. Thay đổi giá trị của phần tử h-captcha-response và g-recaptcha-response là token bạn nhận được từ request yêu cầu giải captcha:

   ```
   document.getElementById("h-captcha-response").innerHTML="TOKEN_FROM_ANTICAPTCHA";
   document.getElementById("g-recaptcha-response").innerHTML="TOKEN_FROM_ANTICAPTCHA";
   ```
2. Thực hiện action mà cần được thực hiện trên trang sau khi giải quyết hCAPTCHA.\
   Thông thường, có một form cần được submit và bạn cần xác định form theo id hoặc name hoặc bất kỳ thuộc tính nào khác rồi submit form. Dưới đây là một vài ví dụ:

   ```
   document.getElementById("hcaptcha-demo-form").submit(); //by id "hcaptcha-demo-form"
   document.getElementsByName("myFormName")[0].submit(); //by element name "myFormName"
   document.getElementsByClassName("example").submit(); //by class name "example"
   ```

   \
   Hoặc đôi khi có một hàm callback được thực thi khi hCAPTCHA được giải quyết.\
   Hàm callback thường được định nghĩa trong tham số `data-callback` của hCAPTCHA, ví dụ:

   ```
   data-callback="myCallbackFunction"
   ```

   \
   Hoặc thỉnh thoảng nó được định nghĩa như tham số `callback` của hàm `grecaptcha.render` , ví dụ:<br>

   ```
   grecaptcha.render('example', {
     'sitekey' : 'someSitekey',
     'callback' : myCallbackFunction,
     'theme' : 'dark'
   });
   ```

   Và tất cả những gì bạn phải làm là gọi hàm đó:

   ```
   myCallbackFunction();
   ```
3. Thì đấy! Bạn đã làm điều đó chỉ với 2 chuỗi code.

**Phương thức 2: thay đổi HTML:**

1. Cut cái div chứa hCAPTCHA từ body trang.

   ```html
   <div style="visibility: hidden; position: absolute; width:100%; top: -10000px; left: 0px; right: 0px; transition: visibility 0s linear 0.3s, opacity 0.3s linear; opacity: 0;">
       <div style="width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; z-index: 2000000000; background-color: #fff; opacity: 0.5;  filter: alpha(opacity=50)">
       </div>
       <div style="margin: 0 auto; top: 0px; left: 0px; right: 0px; position: absolute; border: 1px solid #ccc; z-index: 2000000000; background-color: #fff; overflow: hidden;">
           <iframe src="https://www.google.com/recaptcha/api2/bframe?hl=en&amp;v=r20170213115309&amp;k=6LfP0CITAAAAAHq9FOgCo7v_fb0-pmmH9VW3ziFs#zglq3yifgkmj" title="recaptcha challenge" style="width: 100%; height: 100%;" scrolling="no" name="zglq3yifgkmj" frameborder="0"></iframe>
       </div>
   </div>
   ```

2. Cut toàn bộ khối:

   ```html
   <div class=""><!-- BEGIN: ReCAPTCHA implementation example. -->
       <div id="recaptcha-demo" class="g-recaptcha" data-sitekey="6LfP0CITAAAAAHq9FOgCo7v_fb0-pmmH9VW3ziFs" data-callback="onSuccess" data-bind="recaptcha-demo-submit">
           <div class="grecaptcha-badge" style="width: 256px; height: 60px; transition: right 0.3s ease 0s; position: fixed; bottom: 14px; right: -186px; box-shadow: 0px 0px 5px gray;">
               <div class="grecaptcha-logo">
                   <iframe src="https://www.google.com/recaptcha/api2/anchor?k=6LfP0CITAAAAAHq9FOgCo7v_fb0-pmmH9VW3ziFs&amp;co=aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbTo0NDM.&amp;hl=en&amp;v=r20170213115309&amp;size=invisible&amp;cb=uror1hlow5a" title="recaptcha widget" scrolling="no" name="undefined" width="256" height="60" frameborder="0"></iframe>
               </div>
               <div class="grecaptcha-error"></div>
               <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; "></textarea>
           </div>
       </div>
       <script>
       var onSuccess = function (response) {
           var errorDivs = document.getElementsByClassName("recaptcha-error");
           if (errorDivs.length) {
               errorDivs[0].className = "";
           }
           var errorMsgs = document.getElementsByClassName("recaptcha-error-message");
           if (errorMsgs.length) {
               errorMsgs[0].parentNode.removeChild(errorMsgs[0]);
           }
           document.getElementById("recaptcha-demo-form").submit();
       };
       </script><!-- Optional noscript fallback. -->
       <!-- END: ReCAPTCHA implementation example. -->
   </div>
   ```

3. Đặt những dòng code dưới đây thay cho khối bạn vừa cắt

   ```html
   <input type="submit">
   <textarea name="g-recaptcha-response">%g-recaptcha-response%</textarea>
   ```

   Chỗ *%g-recaptcha-response%* - là token nhận được khi giải hCAPTCHA.\
   Chỗ *%h-captcha-response%* - là token nhận được khi giải hCAPTCHA.

4. Bạn sẽ nhìn thấy nút “Submit query”.\
   Nhấn nút để submit form có h-captcha-response và tất cả dữ liệu form khác tới website.

### *<mark style="color:red;">Lưu ý</mark>:* Nên sử dụng phần mềm PostMan để kiểm tra chạy test trước


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.anticaptcha.top/su-dung-api/api-giai-captcha/hcaptcha.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
