1.Open API Development Guide
1.1 Usage
- Install and log in to the DICloak Antidetect Browser.
- Navigate to More> Settings > Open API to obtain the local interface URL and API key.
- Download the sample code and execute the script.
1.2 Interface Usage Overview
- When the request method is POST, parameters must be passed in the request body as JSON, not as form-data or URL query parameters.
- When the request method is GET, parameters must be passed as form-data.
- For PUT/PATCH/DELETE requests, refer to the specific interface documentation for parameter requirements.
- A successful response with HTTP status 200 will return a JSON object with
code
set to0
. If there is additional data, it will be included in thedata
object. - Responses with non-200 HTTP statuses indicate errors. The error code will correspond to the HTTP status code, and a
code
other than0
indicates failure. The error message will be included in themsg
field, with error codes as referenced in the table below. - For all interface requests, the URL must be appended with “/openapi“. See the sample code for details.
{
'code': 0,
'msg': 'success',
'data': {
'serial_number': 2,
'pid': '27028',
'debug_port': 17539
}
}
{
'code': 500,
'msg': 'fetch fail'
}
- All requests must include the header
X-API-KEY
, with the value set to the API key copied from the client.
1.3 Python Sample Code
The DICloak browser is built on the Chromium 120 kernel, so you need to download the appropriate version of chromedriver before using it.
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# port and api key
BASE_URL = "http://127.0.0.1:52140/openapi"
API_KEY = "*****"
def open_browser(port):
"""
hold profile
:param port:
:return:
"""
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('debuggerAddress', f"127.0.0.1:{port}")
s = Service(r"G:\chromedriver.exe")
browser = webdriver.Chrome(options=chrome_options, service=s)
#示例url代码
browser.get("https://www.zhihu.com")
def get_evn_list():
headers = {
"X-API-KEY": API_KEY,
}
params = {
"page_no": 1,
"page_size": 20
}
response = requests.get(f"{BASE_URL}/v1/env/list", params=params, headers=headers)
return response.json()
def open_env(env_id):
"""
open profile
:param env_id: id
:return:
"""
headers = {
"X-API-KEY": API_KEY,
}
response = requests.patch(f"{BASE_URL}/v1/env/{env_id}/open", headers=headers)
return response.json()
def close_env(env_id):
"""
close profile
:param env_id:
:return:
"""
headers = {
"X-API-KEY": API_KEY,
}
response = requests.patch(f"{BASE_URL}/v1/env/{env_id}/close", headers=headers)
return response.json()
if __name__ == '__main__':
resp = get_evn_list()
if resp['code'] != 0:
print(resp.msg)
exit(0)
# profile list
list = resp['data']['list']
if len(list) == 0:
print('no profile')
exit(0)
print(f"open profile:{list[0]['id']}")
open_resp = open_env(list[0]['id'])
if open_resp['code'] != 0:
print(open_resp['msg'])
exit(0)
print(f"open_resp : {open_resp}")
# Automatic
open_browser(open_resp['data']['debug_port'])
time.sleep(3)
# close profile
close_env(list[0]['id'])
1.4 Java Sample Code
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Main {
// 端口和API Key,替换为实际的值
private static final String BASE_URL = "http://127.0.0.1:52140/openapi";
private static final String API_KEY = "****";
public static void main(String[] args) {
JSONObject resp = getEvnList();
if (resp.getInt("code")!= 0) {
System.out.println(resp.getString("msg"));
System.exit(0);
}
JSONArray list = resp.getJSONObject("data").getJSONArray("list");
if (list.length() == 0) {
System.out.println("no profile");
System.exit(0);
}
System.out.println("open profile:" + list.getJSONObject(0).getString("id"));
JSONObject openResp = openEnv(list.getJSONObject(0).getString("id"));
if (openResp.getInt("code")!= 0) {
System.out.println(openResp.getString("msg"));
System.exit(0);
}
System.out.println("open_resp : " + openResp);
// 打开浏览器
WebDriver browser = openBrowser(openResp.getJSONObject("data").getString("debug_port"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭环境
closeEnv(list.getJSONObject(0).getString("id"));
// 关闭浏览器
browser.quit();
}
public static JSONObject getEvnList() {
try {
URL url = new URL(BASE_URL + "/v1/env/list?page_no=1&page_size=20");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static JSONObject openEnv(String envId) {
try {
URL url = new URL(BASE_URL + "/v1/env/" + envId + "/open");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("PATCH");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static JSONObject closeEnv(String envId) {
try {
URL url = new URL(BASE_URL + "/v1/env/" + envId + "/close");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("X-API-KEY", API_KEY);
connection.setRequestMethod("PATCH");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
StringBuilder response = new StringBuilder();
while (scanner.hasNextLine()) {
response.append(scanner.nextLine());
}
scanner.close();
return new JSONObject(response.toString());
}
throw new RuntimeException("请求失败,状态码: " + responseCode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static WebDriver openBrowser(String port) {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("debuggerAddress", "127.0.0.1:" + port);
return new ChromeDriver(chromeOptions);
}
}