手机访问网站获取外网IP

来源:互联网 发布:ie60和ie80知乎 编辑:程序博客网 时间:2024/04/25 02:40

哈哈,这个说白了就是手机后台访问网站,再解析获得的数据

app: http://fir.im/dpxu 可以下载看看效果

书接上文:http://blog.csdn.net/i_do_can/article/details/50421882

android手机联网,所以开启一个异步任务执行后台程序

public class IpAddressInfo extends AsyncTask<String, Integer, StringBuffer>
现在推荐的一种手机访问网络获取网页数据的方式是:URLConnection
给一个网址,有get .post 方式连接网络,如果只是一个网址没多少数据,这俩是一样的,post 可以传的参数数据量多一些

给出两种方式联网的函数

public void doGetURL(String url) throws Exception {        URL localURL = new URL(url);        URLConnection connection = localURL.openConnection();        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");        if (httpURLConnection.getResponseCode() >= 300) {            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());        }        inputStream = httpURLConnection.getInputStream();        inputStreamReader = new InputStreamReader(inputStream);        reader = new BufferedReader(inputStreamReader);        while ((tempLine = reader.readLine()) != null) {            tempHTML.append(tempLine);        }    }    private void doPostURL(String url) throws Exception{        URL localURL = new URL(url);        URLConnection connection = localURL.openConnection();        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;        httpURLConnection.setDoOutput(true);        httpURLConnection.setRequestMethod("POST");        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(url.length()));        outputStream = httpURLConnection.getOutputStream();        outputStreamWriter = new OutputStreamWriter(outputStream);        outputStreamWriter.write(url.toString());        outputStreamWriter.flush();        if (httpURLConnection.getResponseCode() >= 300) {            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());        }        inputStream = httpURLConnection.getInputStream();        inputStreamReader = new InputStreamReader(inputStream);        reader = new BufferedReader(inputStreamReader);        while ((tempLine = reader.readLine()) != null) {            tempHTML.append(tempLine);        }    }

我访问的是https://www.apnic.net/apnic-info/whois_search/your-ip

这个网页很奇怪,他的IP 我一直 获取不出来,只能得到IP的其他相关信息,而且网址也不是这个,我给出他的那个网址:

这只是我个人的获取,如果涉及到相关利益,我表示很抱歉,希望大家不要把这个网址商业使用


IP的获取淘宝有个貌似高大上的方式,就是极度不稳定:http://ip.taobao.com/service/getIpInfo2.php?ip=myip

之所以说看着”高大上“,是因为返回的是JSON串,需要用JSONObject解析,加上”貌似“是因为太不稳定啦

后台调用的代码:

public static String GetNetIp2() throws  Exception{        String IP = "";        String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";        URL url = new URL(address);        HttpURLConnection connection = (HttpURLConnection) url                .openConnection();        connection.setUseCaches(false);        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)        {            InputStream in = connection.getInputStream();            // 将流转化为字符串            BufferedReader reader = new BufferedReader(                    new InputStreamReader(in));            String tmpString = "";            StringBuilder retJSON = new StringBuilder();            while ((tmpString = reader.readLine()) != null)            {                retJSON.append(tmpString + "\n");            }            JSONObject jsonObject = new JSONObject(retJSON.toString());            String code = jsonObject.getString("code");            if (code.equals("0"))            {                JSONObject data = jsonObject.getJSONObject("data");                IP = data.getString("ip") + "(" + data.getString("country")                        + data.getString("area") + "区"                        + data.getString("region") + data.getString("city")                        + data.getString("isp") + ")";            }            else            {                IP = "";            }        }        else        {            IP = "";        }        return IP;    }

无意中解析网站源码时发现这样一个网址:http://1212.ip138.com/ic.asp,同样的,这只是我个人的获取,只是我个人的测试使用,非商业,如果涉及到相关利益,我表示很抱歉,希望大家不要把这个网址商业使用

获取IP就极其简单啦

 <span style="white-space:pre"></span>line = null;            // 从反馈的结果中提取出IP地址             int start = strber.indexOf("[");             int end = strber.indexOf("]" );            line =strber.substring(start + 1 ,end);

对了,异步任务处理完后

<span style="white-space:pre"></span>/** * onPostExecute方法用于在执行完后台任务后更新UI,显示结果  EditText */@Overrideprotected void onPostExecute(StringBuffer result) {// 判断是否为null,若不为null,则在页面显示HTML代码if (result != null) {<span style="white-space:pre"></span>edtHTTP.setText(result);}super.onPostExecute(result);}
OK ,基本完成啦


源码下载:http://download.csdn.net/detail/i_do_can/9381118

0 0