java调用站长之家api查询whois

该代码示例展示了如何使用Java通过HttpURLConnection类执行GET请求,从站长之家API获取Whois信息。首先,构建请求URL,包含必要的参数如key和domain。然后,设置请求头和超时时间,最后读取并返回响应结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文


import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class Demo3 {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("https://apidatav2.chinaz.com/single/whois");
        Map<String,String> params = new HashMap<String,String>();
        params.put("key","0ec0a2fb7fb04a099ddac0xxxxxxxxx");
        params.put("domain","baidu.com");

        String result = GetPostUrl(sb.toString(),params,"GET",null,0,0);
        System.out.println(result);
    }

    public static String GetPostUrl(String sendUrl,Map<String,String> params,String sendType,String charset,
                                    int repeat_request_count,int repeat_request_max_count) {
        URL url = null;
        HttpURLConnection httpURLConnection = null;

        try {
            // 构建请求参数
            StringBuffer paramSb = new StringBuffer();
            if (params != null) {
                for (Map.Entry<String, String> e : params.entrySet()) {
                    paramSb.append(e.getKey());
                    paramSb.append("=");
                    // 将参数值urlEncode编码,防止传递中乱码
                    paramSb.append(URLEncoder.encode(e.getValue(), "UTF-8"));
                    paramSb.append("&");
                }
                paramSb.substring(0, paramSb.length() - 1);
            }
            url = new URL(sendUrl + "?" + paramSb.toString());
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            // 设置http请求超时时间30000毫秒(30秒)
            httpURLConnection.setConnectTimeout(30000);
            httpURLConnection.setReadTimeout(30000);
            httpURLConnection.setUseCaches(true);

            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                DataInputStream in = new DataInputStream(httpURLConnection.getInputStream());
                int len = in.available();
                byte[] by = new byte[len];
                in.readFully(by);
                String rev = new String(by, "UTF-8");

                in.close();

                return rev;
            } else {
                return "<?xml version=\"1.0\" encoding=\"utf-8\" ?><error>发送第三方请求失败</error>";
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return null;
    }
}

用到的key可以去站长之家注册申请

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值