根据IP地址获取当前城市,总是获取不到

Android 码拜 8年前 (2016-03-28) 1635次浏览
infoUrl = new URL(“http://www.ip138.com/ip2city.asp”);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection 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”);
inStream.close();
// 从反馈的结果中提取出IP地址
int start = strber.indexOf(“[“);
int end = strber.indexOf(“]”, start + 1);
strForeignIP = strber.substring(start + 1, end);
getCityByIp();
}
} catch (Exception e)
{
e.printStackTrace();
}
里边的responseCode 返回的总是503,这个网址在浏览器里打开就能获取到当前的IP地址!
求指导为什么会这样!
解决方案

10

getCityByIp();是怎么实现 的?
假如是从第三方,那说明第三方拒绝请求的呀~

10

本人试了,这个IP获取的页面是有一个安全检查在先,代码这样访问通过不了安全检查,所以,503.
换别的方式获取IP吧。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明根据IP地址获取当前城市,总是获取不到
喜欢 (0)
[1034331897@qq.com]
分享 (0)