手机访问网站获取外网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
- 手机访问网站获取外网IP
- 获取访问网站客户ip地址
- 手机端获取外网IP方法
- 外网访问内网WEB网站(适合动态IP)
- 安卓获取手机外网ip地址!!
- 通过InterNet,获取本机访问外网IP
- 获取android手机ip
- 如何获取手机IP
- android获取手机ip
- android获取手机ip
- 获取手机ip地址
- 获取手机ip地址
- android获取手机ip
- 获取手机上网ip
- 获取手机ip
- 获取手机IP地址
- 获取手机ip地址
- 获取手机Ip地址
- Spring定时任务的几种实现
- modelsim仿真波形理解
- 使用Keil MDK运行第一个STM32程序
- 自定义布局,堆叠布局来袭!
- vs2012 不能正确加载vc++ package
- 手机访问网站获取外网IP
- 人生如果是十分…
- JAVA正则表达式 Pattern和Matcher
- nyoj--27--水池数目(dfs)
- PLSQL连接远程Oracle
- 小米Pad退出开发者模式
- 用两个栈实现队列&&用两个队列实现一个栈
- 架构师于小波:魅族实时消息推送架构
- NYOJ 266 字符串逆序输出