Fork me on GitHub

根据ip获取城市名

没有什么说的,参考此文: http://blog.csdn.net/hhzichen/article/details/8098536
复制代码
package cn.com.jsoft.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  版权所有 All Rights Reserved
 * 
 * @author Irving
 * @description 获得该IP对应的城市
 * @date 2013年6月28日13:48:38
 * @version 1.0.0
 * @email zhouyongtao@outlook.com
 */
public class HttpIpAddress {

    /**
     * 支持普通HTTP请求 WCF WEBSERVICE
     * 
     * @param url
     * @param xml
     *            WCF XML参数
     * @param method
     * @param contentType
     * @param timeOut
     * @return
     */
    public static String HttpProxy(String url, String xml, String method, String contentType, String timeOut) {
        OutputStream os = null;
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        StringBuffer sb = null;
        try {
            if (StringUtils.isEmpty(method)) {
                method = "GET";
            }
            if (StringUtils.isEmpty(contentType)) {
                contentType = "application/x-www-form-urlencoded";// "application/x-java-serialized-object"
            }
            if (StringUtils.isEmpty(timeOut)) {
                timeOut = "5000";
            }
            URL _url = new URL(url);
            conn = (HttpURLConnection) _url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod(method);
            conn.setRequestProperty("Content-type", contentType);
            conn.setConnectTimeout(Integer.parseInt(timeOut));
            // conn.setRequestProperty("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            // conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            // conn.setRequestProperty("Content-Type", "text/xml;charset=GBK");
            // 请求参数
            if (!StringUtils.isEmpty(xml)) {
                os = conn.getOutputStream();
                os.write(xml.getBytes());
                os.flush();
            }
            // 返回值
            if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
                conn.connect();
                // InputStreamReader inReader = new InputStreamReader(is,
                // "UTF-8");
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
                sb = new StringBuffer();
                String str = null;
                while ((str = reader.readLine()) != null) {
                    sb.append(str + "\n");
                }
            }
            return sb.toString();
        } catch (IOException ex) {
            // 发邮件
        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
                if (reader != null) {
                    reader.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "北京";
    }

    /**
     * 获得该IP对应的城市
     * 
     * @param ipAddress
     *            IP地址
     * @return
     */
    public static String getIpAddress(String ipAddress) {
        String url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + ipAddress;// 210.51.45.99
        return HttpProxy(url, "", "", "", "");
    }

    public static void main(String[] args) {
        String result = getIpAddress("210.51.45.99");
        if (!result.endsWith("北京")) {
            Matcher m = Pattern.compile("<location>.*?</location>").matcher(result);
            while (m.find()) {
              System.out.println(m.group());
            }
        }
    }
}
复制代码
posted @   花儿笑弯了腰  阅读(497)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示