Android 获取外网IP,实测有效

网上有很多获取IP的例子,不过都是获取到的本地ip,还有的是因为走不通了,获取到的ip为空,下面看实测获取到外网IP的代码,注意需要在线程里面执行

/**
     * 获取外网的IP(要访问Url,要放到后台线程里处理)
     *
     * @param @return
     * @return String
     * @throws
     * @Title: GetNetIp
     * @Description:
     */
    public static String getNetIp() {
        URL infoUrl = null;
        InputStream inStream = null;
        String ipLine = "";
        HttpURLConnection httpConnection = null;
        try {
//            infoUrl = new URL("http://ip168.com/");
            infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
            URLConnection connection = infoUrl.openConnection();
            httpConnection = (HttpURLConnection) connection;
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inStream = httpConnection.getInputStream();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inStream, "utf-8"));
                StringBuilder strber = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null){
                    strber.append(line + "\n");
                }
                Pattern pattern = Pattern
                        .compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");
                Matcher matcher = pattern.matcher(strber.toString());
                if (matcher.find()) {
                    ipLine = matcher.group();
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inStream.close();
                httpConnection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        Log.e("getNetIp", ipLine);
        return ipLine;
    }
时间: 2024-08-06 07:54:48

Android 获取外网IP,实测有效的相关文章

linux获取外网ip

引言:目前获取ip的方法中,ifconfig和ip获取函数得到的都是内网ip.有时候需要获取外网ip,目前通用的做法,是向外部服务器发送请求,解析外部服务器响应,从而得到的自己的外网ip.linux下的 curl可以替我们完成这些工作,当然,不怕麻烦的话,可以自己分析http协议,自己实现以上过程.如果熟悉python的话,那就更简单了,就像我们所知道的,python总是有现成的库函数可供我们调用.一下总结几种获取外网ip的方法,以供查询,资料来源互联网. 参看资料: http://www.cn

C#获取外网IP、本机MAC地址及Ping的实现

原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表达式提取出IP来 class Program { static void Main(string[] args) { Console.WriteLine(GetExportIP()); Console.ReadKey(); } public static string GetExportIP() {

c#获取外网IP地址的方法

1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超时(秒),当宽带未连接或者连接中的时候获取不到IP public static string GetIP_PPPOE(int timeout) { int i = timeout * 2; while (i > 0) { try { NetworkInterface[] nics = Network

Android获取外网和内网的IP

很晚了,直播上代码: /** * 获取外网的IP(要访问Url,要放到后台线程里处理) * * @Title: GetNetIp * @Description: * @param @return * @return String * @throws */ public static String GetNetIp() { URL infoUrl = null; InputStream inStream = null; String ipLine = ""; HttpURLConnect

MFC C++ 获取外网IP地址

#include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { UINT nCodePage = 936; //GB2312 int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0); wchar_t* pBuffer = new wchar_t[nLength+1]; MultiByteToWide

python获取外网IP并发邮件

第一步:通过ip138来爬取外网ip 第二部:通过python的smtplib模块和email来发送邮件,具体用法去网上搜索,下面是代码示例: #!/usr/bin/env python #coding:utf-8 import urllib2 import re import smtplib from email.MIMEText import MIMEText from email.Header import Header ##################################

linux下获取外网IP

使用阿里云或者有多个网卡IP的机器需要取外网IP时,可以用下面这种 wget -qO - ifconfig.co 更多方法参考:https://yq.aliyun.com/ziliao/105999

MVC 如何获取外网IP

1 public string GetIP() 2 { 3 string tempIp = ""; 4 WebRequest wr = WebRequest.Create("http://city.ip138.com/ip2city.asp"); 5 Stream s = wr.GetResponse().GetResponseStream(); 6 StreamReader sr = new StreamReader(s, Encoding.Default); 7

C# Winform程序获取外网IP地址

string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); System.Net.WebRequest wr = System.Net.WebRequest.Create(uri); System.IO.Stream s = wr.GetResponse().GetResponseStream(); System.IO.StreamReader sr = new Sy