手机访问网站获取外网IP
哈哈,这个说白了就是手机后台访问网站,再解析获得的数据
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
手机访问网站获取外网IP相关推荐
- 如何获取外网IP地址
1. 获取外网IP的方法: 获取外网IP地址方法: 访问网站:http://www.3322.org/dyndns/getip 访问网站:http://city.ip138.com/ip2city. ...
- 怎样查询本机ip地址?如何利用花生壳获取外网IP教程
听到ip地址这个概念,想必大家都不会陌生,如果我们想要跟对方进行通信,通常我们需要知道对方的ip地址,另外在网络通信中,还有一个非常重要的地址,那就是mac地址.那么我们如何知道本机ip地址呢?我们如 ...
- 获取外网IP,再根据IP定位城市
根据ip定位城市对外开放的接口,有些接口不能直接用了,比如说淘宝接口.这里我用的是一个不知名的接口. 1.AddressUtil工具类 package cn.ztjz.module.audit.uti ...
- python切换ip群发邮件_python获取外网IP并发邮件的实现方法
第一步:通过ip138来爬取外网ip 第二步:通过python的smtplib模块和email来发送邮件,具体用法去网上搜索, 下面是代码示例: #!/usr/bin/env python #codi ...
- delphi获取外网IP
delphi获取外网IP function getIp():string; var url:string; myIdHttp:TIdHTTP; data:ISuperObject; beginResu ...
- python获取公网ip的几种方式_python获取外网ip地址的方法总结
本文实例总结了python获取外网ip地址的方法.分享给大家供大家参考.具体如下: 一.利用脚本引擎库直接获取 import console; import web.script import ine ...
- vue中获取外网IP的方法
记一下如何在vue项目中获取外网IP 之前有篇文章js获取客户端真实IP记录了如何获取外网IP 那个只是在普通html页面当中的获取方法,在vue项目中就不能这么使用了. 这里介绍一种适用于vue项目 ...
- 如何从HttpServletRequest中获取外网IP地址
如何从HttpServletRequest中获取外网IP地址 Java 中可以通过HttpServletRequest类的getRemoteAddr(); //可能是用户真是ip也可能是代理服务器的i ...
- 获取外网IP地址API
1.获取外网IP地址 地址: http://pv.sohu.com/cityjson?ie=utf-8 返回结果: var returnCitySN = {"cip": " ...
最新文章
- gprs发送信号对方如何接收_和接收缓冲区比较:Netty发送缓冲区是如何设计的,why?...
- wifi管理系统_KJ725(A)精确人员定位管理系统实现对井下人员和车辆的精确定位
- android离散点贝塞尔曲线,离散点拟合曲线贝塞尔曲线B样条.ppt
- 观察者模式/ java实现附代码 /
- java---数字排序
- Linux中 set、env、declare、export显示shell变量的区别
- linux下wxr的权限,Day16-Linux权限知识和应用实践讲解
- 修罗武神正版游戏服务器,修罗武神正版游戏官网
- java 锁的类型_Java锁的种类 - shawnplaying的个人页面 - OSCHINA - 中文开源技术交流社区...
- [设计模式]桥接模式
- Androidの网络Http之判断是否连接服务器
- Linux进程学习(孤儿进程和守护进程)
- 【ZJOI 2018】 历史(lct)
- excel2010服务器打开闪退
- hdoj 5510 Bazinga
- 【Python】开发笔记
- tp路由器 拨号失败 服务器无响应,tp路由器wdr8500拨号不成功怎么办
- python验证身份证号码大全_国服魔兽注册账号需要验证身份证及真实姓名
- C语言麻将递归,【算法SQL面试题】面试问题:麻将胡牌递归算… - 看准网
- c语言 最小公倍数与最大公因数