一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java調用淘寶api聯網查詢ip歸屬地

java調用淘寶api聯網查詢ip歸屬地

2019-11-14 15:29java教程網 JAVA教程

java聯網查詢IP歸屬地,原理是根據淘寶提供的service查詢IP的歸屬地并且解析http請求返回的json串

淘寶返回的數據為:
{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c","area_id":"300000","region":"\u5c71\u4e1c\u7701","region_id":"370000","city":"\u4e1c\u8425\u5e02","city_id":"370500","county":"","county_id":"-1","isp":"\u8054\u901a","isp_id":"100026","ip":"60.214.183.158"}}

用法:

復制代碼代碼如下:

String arr1 = GetAddressByIp.GetAddressByIp("120.192.182.1");
System.out.println(arr1);



詳細請看代碼

 

 

復制代碼代碼如下:


package getAddressByIp;

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

public class GetAddressByIp
{

 
 /**
  * 
  * @param IP
  * @return
  */
 public static String GetAddressByIp(String IP){
  String resout = "";
  try{
   String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
   System.out.println(str);

   JSONObject obj = JSONObject.fromObject(str);
   JSONObject obj2 =  (JSONObject) obj.get("data");
   String code = (String) obj.get("code");
   if(code.equals("0")){

    resout =  obj2.get("country")+"--" +obj2.get("area")+"--" +obj2.get("city")+"--" +obj2.get("isp");
   }else{
    resout =  "IP地址有誤";
   }
  }catch(Exception e){

   e.printStackTrace();
    resout = "獲取IP地址異常:"+e.getMessage();
  }
  return resout;

 }

    public static String getJsonContent(String urlStr)
    {
        try
        {// 獲取HttpURLConnection連接對象
            URL url = new URL(urlStr);
            HttpURLConnection httpConn = (HttpURLConnection) url
                    .openConnection();
            // 設置連接屬性
            httpConn.setConnectTimeout(3000);
            httpConn.setDoInput(true);
            httpConn.setRequestMethod("GET");
            // 獲取相應碼
            int respCode = httpConn.getResponseCode();
            if (respCode == 200)
            {
                return ConvertStream2Json(httpConn.getInputStream());
            }
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return "";
    }
    private static String ConvertStream2Json(InputStream inputStream)
    {
        String jsonStr = "";
        // ByteArrayOutputStream相當于內存輸出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        // 將輸入流轉移到內存輸出流中
        try
        {
            while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
            {
                out.write(buffer, 0, len);
            }
            // 將內存流轉換為字符串
            jsonStr = new String(out.toByteArray());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonStr;
    }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 接吻吃胸摸下面啪啪教程 | 欧美黑人ⅹxxx片 | 香蕉久久夜色精品国产小优 | 精品一区二区三区在线播放 | 第一次不是你高清在线观看 | 黑人巨鞭大战白妞10级 | 2021国产麻豆剧传媒新片 | 久久精品一区二区三区资源网 | 成人伊人亚洲人综合网站222 | 国产精品合集久久久久青苹果 | 久久久精品成人免费看 | 国色天香社区视频免费高清在线观看 | 亚洲精品国产一区二区三区在 | 色综合久久夜色精品国产 | 国产一卡2卡3卡4卡公司科普 | 国产精品一区二区三区久久 | 四虎影院免费在线播放 | 国产在线观看一区 | www.91在线视频| 太紧太深了受不了黑人 | 四虎1515hh.com| 免费看美女被靠到爽的视频 | 狠狠色狠狠色综合日日小蛇 | 黑人群性xxx | 91香蕉国产在线观看人员 | 国产亚洲精品一区二区在线观看 | 国产自在自线午夜精品之la | 日韩一级免费毛片 | 五月最新商场女厕所高跟嘘嘘 | xxxxx性欧美| 亚洲伦理视频 | 亚洲国产综合久久久无码色伦 | 热色综合 | 亚洲成人黄色 | 秋霞一级成人欧美理论 | 被高跟鞋调教丨vk | 国产亚洲精品网站 | 国产亚洲欧美日韩俺去了 | 国产福利在线观看第二区 | 国色天香社区视频在线观看免费完整版 | tiny4k欧美极品在线 |