zoukankan      html  css  js  c++  java
  • 判断ip是否在中国内地

    package com.example.countrytest;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;
    
    /*
     * 通过ip判断是否是中国内地
     */
    public class IpChina {
        
        private static final String ipUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
        private static final String China = "u4e2du56fd";
        private static final String Taiwan = "u53f0u6e7e";
        private static final String HongKong = "u9999u6e2f";
        private static final String Macao = "u6fb3u95e8";
        
        public static boolean ipIsChinaInland() {
            try {
                String json = getIpData();
                System.out.println("json="+json);
                JSONObject jsonObject = new JSONObject(json);
                String country = jsonObject.getString("country");
                String city = jsonObject.getString("province");
                if(country!=null && city!=null) {
                    if(country.equals(China)) {
                        if(city.equals(Taiwan) || city.equals(HongKong) || city.equals(Macao)) {
                            // deal with non-china inland
                        } else {
                            //  deal with china inland
                            return true;
                        }
                    } else {
                        //  deal with non-china inland
                    }
                } else {
                    // deal with non-china inland
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
        
        private static String getIpData() throws Exception  {
            HttpGet httpRequest = new HttpGet(ipUrl);// 建立http get联机
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 发出http请求
            if (httpResponse.getStatusLine().getStatusCode() == 200)
               return EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串
            return null;
        }
        
    }
  • 相关阅读:
    【杭电】[1874]畅通工程续
    【杭电】[2544]最短路
    【杭电】[1087]Super Jumping! Jumping! Jumping!
    【HPU】[1689]MZY寻宝
    【杭电】[1495]非常可乐
    【杭电】[1242]Rescue
    【杭电】[1787]GCD Again
    【算法】欧拉函数——小于n的数中与n互质数的数目
    【HPU】[1738]Stack ? Queue ?
    【HPU】[1737]老王特警队
  • 原文地址:https://www.cnblogs.com/leng-yuye/p/3184330.html
Copyright © 2011-2022 走看看