介绍
本教程提供2025年7月最新版的Java代码,用于检测域名在微信平台内的封禁状态。通过调用微信官方API接口,可以获取三种状态信息:
- status为2表示域名正常
- status为1表示域名被拦截
- status为0表示域名被封禁
Java代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
public class WeChatDomainChecker {
private static final String VERSION = "2025.07";
private static final String API_URL = "https://api.wxapi.work/wx/";
private static final int MAX_RETRIES = 3;
private static final int CACHE_TIME = 1800; // 30分钟缓存
private static final int TIMEOUT = 10000; // 10秒超时
// 状态常量
public static final int STATUS_NORMAL = 2;
public static final int STATUS_WARNING = 1;
public static final int STATUS_BANNED = 0;
public static final int STATUS_ERROR = -1;
private static final Map<String, CacheEntry> cache = new HashMap<>();
public static void main(String[] args) {
// 示例使用
System.out.println("微信域名封禁状态检测报告(2025年7月)");
System.out.println("=".repeat(60));
// 单域名检测
System.out.println("\\n单域名检测示例:");
DomainResult result = checkDomain("weixin.qq.com");
printResult(result);
// 批量检测
System.out.println("\\n批量检测示例:");
String[] domains = {"weixin.qq.com", "baidu.com", "example.com", "invalid.domain", ""};
for (String domain : domains) {
DomainResult domainResult = checkDomain(domain);
printResult(domainResult);
try {
Thread.sleep(500); // 避免请求过于频繁
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public static DomainResult checkDomain(String domainOrUrl) {
// 标准化域名/URL
String domain = normalizeDomain(domainOrUrl);
if (domain == null) {
return new DomainResult(STATUS_ERROR, domainOrUrl, "无效的域名格式", getCurrentTimestamp());
}
// 检查缓存
CacheEntry cachedResult = cache.get(domain.toLowerCase());
if (cachedResult != null &&
System.currentTimeMillis() – cachedResult.timestamp < CACHE_TIME * 1000) {
return cachedResult.result;
}
// 执行检测
DomainResult result = doCheckWithRetry(domain);
// 缓存结果
cache.put(domain.toLowerCase(), new CacheEntry(result, System.currentTimeMillis()));
return result;
}
private static DomainResult doCheckWithRetry(String domain) {
for (int attempt = 0; attempt < MAX_RETRIES; attempt++) {
try {
String encodedDomain = URLEncoder.encode(domain, StandardCharsets.UTF_8);
URL url = new URL(API_URL + "?url=" + encodedDomain);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "WeChatDomainChecker/" + VERSION);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-Requested-With", "Java");
connection.setConnectTimeout(TIMEOUT);
connection.setReadTimeout(TIMEOUT);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
return parseResponse(response.toString(), domain);
}
} else {
if (attempt == MAX_RETRIES – 1) {
return new DomainResult(STATUS_ERROR, domain,
"API请求失败: HTTP " + responseCode, getCurrentTimestamp());
}
}
} catch (IOException e) {
if (attempt == MAX_RETRIES – 1) {
return new DomainResult(STATUS_ERROR, domain,
"API请求失败: " + e.getMessage(), getCurrentTimestamp());
}
}
try {
Thread.sleep(1000); // 重试间隔
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return new DomainResult(STATUS_ERROR, domain,
"请求被中断", getCurrentTimestamp());
}
}
return new DomainResult(STATUS_ERROR, domain, "请求失败", getCurrentTimestamp());
}
private static DomainResult parseResponse(String jsonResponse, String originalDomain) {
try {
// 简化JSON解析,实际项目中建议使用Gson或Jackson
int status = STATUS_ERROR;
String message = "未知状态";
if (jsonResponse.contains("\\"status\\":\\"2\\"")) {
status = STATUS_NORMAL;
message = "域名正常";
} else if (jsonResponse.contains("\\"status\\":\\"1\\"")) {
status = STATUS_WARNING;
message = "域名被拦截";
} else if (jsonResponse.contains("\\"status\\":\\"0\\"")) {
status = STATUS_BANNED;
message = "域名被封禁";
}
return new DomainResult(status, originalDomain, message, getCurrentTimestamp());
} catch (Exception e) {
return new DomainResult(STATUS_ERROR, originalDomain,
"无效的API响应格式", getCurrentTimestamp());
}
}
private static String normalizeDomain(String domainOrUrl) {
if (domainOrUrl == null || domainOrUrl.trim().isEmpty()) {
return null;
}
String normalized = domainOrUrl.trim();
// 如果没有协议头,添加http://以便解析
if (!normalized.startsWith("http://") && !normalized.startsWith("https://")) {
normalized = "http://" + normalized;
}
try {
URL url = new URL(normalized);
String host = url.getHost();
return host != null && !host.isEmpty() ? host : null;
} catch (Exception e) {
return null;
}
}
private static String getCurrentTimestamp() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
private static void printResult(DomainResult result) {
String status;
switch (result.status) {
case STATUS_NORMAL: status = "✅ 正常"; break;
case STATUS_WARNING: status = "⚠️ 拦截"; break;
case STATUS_BANNED: status = "❌ 封禁"; break;
default: status = "⚠️ 错误";
}
System.out.println("\\n检测结果(" + result.timestamp + ")");
System.out.println("=".repeat(60));
System.out.println("域名: " + (result.domain.isEmpty() ? "[空域名]" : result.domain));
System.out.println("状态: " + status);
System.out.println("详细信息: " + result.message);
System.out.println("=".repeat(60));
}
static class DomainResult {
int status;
String domain;
String message;
String timestamp;
public DomainResult(int status, String domain, String message, String timestamp) {
this.status = status;
this.domain = domain;
this.message = message;
this.timestamp = timestamp;
}
}
static class CacheEntry {
DomainResult result;
long timestamp;
public CacheEntry(DomainResult result, long timestamp) {
this.result = result;
this.timestamp = timestamp;
}
}
}
使用方法
1. 基本使用
public class Main {
public static void main(String[] args) {
// 检测单个域名
DomainResult result = WeChatDomainChecker.checkDomain("baidu.com");
System.out.println("状态: " + result.status);
System.out.println("信息: " + result.message);
// 批量检测
String[] domains = {"weixin.qq.com", "baidu.com", "example.com"};
for (String domain : domains) {
DomainResult domainResult = WeChatDomainChecker.checkDomain(domain);
WeChatDomainChecker.printResult(domainResult);
}
}
}
2. 结果解释
返回结果示例:
{
"status": 2,
"domain": "baidu.com",
"message": "域名正常",
"timestamp": "2025-07-24 00:30:00"
}
或
{
"status": 1,
"domain": "example.com",
"message": "域名被拦截",
"timestamp": "2025-07-24 00:31:00"
}
或
{
"status": 0,
"domain": "test.com",
"message": "域名已被封禁",
"timestamp": "2025-07-24 00:32:00"
}
- status:
- 2: 域名正常
- 1: 域名被拦截
- 0: 域名被封禁
- -1: 检测异常
- domain: 检测的域名
- message: 详细的状态信息
- timestamp: 检测时间戳
2025年7月更新说明
高级功能
1. 自定义配置
// 修改静态常量值
public class WeChatDomainChecker {
private static final int MAX_RETRIES = 5; // 最大重试5次
private static final int CACHE_TIME = 3600; // 缓存1小时
// …其他代码
}
2. 强制刷新缓存
// 清除缓存
WeChatDomainChecker.cache.clear();
DomainResult result = WeChatDomainChecker.checkDomain("baidu.com");
3. 结果筛选
// 筛选被封禁的域名
List<DomainResult> bannedDomains = new ArrayList<>();
String[] domains = {"weixin.qq.com", "baidu.com", "example.com"};
for (String domain : domains) {
DomainResult result = WeChatDomainChecker.checkDomain(domain);
if (result.status == WeChatDomainChecker.STATUS_BANNED) {
bannedDomains.add(result);
}
}
注意事项
示例输出
单域名检测示例:
检测结果(2025-07-24 00:30:00)
============================================================
域名: weixin.qq.com
状态: ✅ 正常
详细信息: 域名正常
============================================================
批量检测示例:
检测结果(2025-07-24 00:30:01)
============================================================
域名: baidu.com
状态: ✅ 正常
详细信息: 域名正常
============================================================
检测结果(2025-07-24 00:30:02)
============================================================
域名: invalid.domain
状态: ⚠️ 错误
详细信息: 无效的域名格式
============================================================
评论前必须登录!
注册