此Java代碼用於批量請求指定的微信域名接口 https://api.52an.fun/wx/?url={url} 來檢查多個微信域名的狀態。接口返回的JSON數據中包含了 status 字段,status 為 1 表示域名正常,0 表示域名被封禁。如果 status 為 0,會有詳細的封禁信息。代碼將根據返回的狀態輸出每個域名的狀態。
Java代碼示例:
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import org.json.JSONObject;
import java.util.List;
import java.util.ArrayList;
public class WeChatDomainChecker {
// 需要檢查的微信域名列表
private static final List<String> domains = new ArrayList<>();
static {
// 將需要檢查的微信域名添加到列表中
domains.add("example.com");
domains.add("wxexample.com");
domains.add("testdomain.com");
// 可以繼續添加更多的域名
}
// 請求域名狀態的 API 接口模板
private static final String API_URL = "https://api.52an.fun/wx/?url=";
public static void main(String[] args) {
// 批量檢查域名的狀態
for (String domain : domains) {
String status = checkDomainStatus(domain);
System.out.println("域名: " + domain + ", 狀態: " + status);
}
}
// 請求接口檢查域名狀態
private static String checkDomainStatus(String domain) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
// 連接API接口
URL url = new URL(API_URL + domain);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 讀取響應數據
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
// 解析返回的JSON
JSONObject jsonResponse = new JSONObject(response.toString());
int status = jsonResponse.getInt("status");
String message = jsonResponse.getString("message");
// 判斷域名的狀態
if (status == 1) {
return "正常";
} else if (status == 0) {
return "被封禁,原因:" + message;
} else {
return "未知狀態";
}
} catch (IOException e) {
return "請求失敗: " + e.getMessage();
} finally {
// 關閉連接
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// 忽略關閉異常
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}
代碼介紹:
- API 請求:此代碼通過
HttpURLConnection類向指定的微信域名狀態接口發送 HTTP GET 請求,查詢每個域名的狀態。API 接口需要傳入域名作為查詢參數。 -
數據解析:接口返回的是 JSON 格式的響應,代碼使用
org.json.JSONObject來解析響應。根據status字段的值來判斷域名的狀態:status = 1表示域名正常。status = 0表示域名被封禁,message字段會包含封禁原因。
- 批量檢查:通過遍歷
domains列表,代碼會批量請求每個域名的狀態。可以根據需求將更多的域名添加到domains列表中。 - 異常處理:如果請求失敗(如網絡問題或接口錯誤),會捕獲
IOException異常並返回相應的錯誤信息。 - 輸出結果:程序會輸出每個域名的狀態。如果域名被封禁,會顯示封禁原因;如果正常,則顯示“正常”狀態。
使用方法:
-
依賴:此代碼使用了
org.json庫來處理 JSON 數據。可以使用 Maven 或 Gradle 添加org.json依賴:-
Maven 依賴:
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20210307</version> </dependency> -
Gradle 依賴:
implementation 'org.json:json:20210307'
-
- 運行程序:將
domains列表中添加需要檢查的域名,然後運行程序,查看每個域名的狀態。
示例輸出:
域名: example.com, 狀態: 正常
域名: wxexample.com, 狀態: 被封禁,原因:存在違規行為
域名: testdomain.com, 狀態: 正常
擴展功能:
- 可以將域名的檢查結果保存到日誌文件或數據庫中,以便後續分析和記錄。
- 可以增加多線程支持,以提高批量檢查時的效率,尤其在域名數量較多時。