http協議:
http://api.youripapi.com/ip/
https協議:
https://api.youripapi.com/ip/
* API接口可能會因為各種網絡原因和攻擊都可能産生阻斷,請開發時做好冗餘和異常處理
* 當HTTP請求返回的狀態碼非200時,請做異常處理,比如 202 狀態碼造成的原因可能是無效Token、餘額不足、格式錯誤
Java調用iP查詢接口示例:
public class QueryHelper {
/**
* txt|jsonp|xml
*/
public static String DATATYPE="text";
public static String get(String urlString,String token) {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("token",token);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
for (String s = br.readLine(); s != null; s = br
.readLine()) {
builder.append(s);
}
br.close();
return builder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String queryIP(String ip){
String url="https://api.youripapi.com/ip/?ip="+ip+"&datatype="+DATATYPE;
String token="859476648b3de65d76804906dd1a1c6a";
return get(url,token);
}
}
//以下是使用示例:
//QueryHelper.queryIP("8.8.8.8");