本文主要是介绍java调用站长之家api查询whois,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文
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可以去站长之家注册申请
这篇关于java调用站长之家api查询whois的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!