09-04
18
web service :IP查询
作者:Java伴侣 日期:2009-04-18
复制内容到剪贴板 程序代码
String ipprovince = "";
String ip = "58.214.5.162";
try {
// URL url=new
// URL("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=58.214.5.162");
URL url = new URL("http://whois.pconline.com.cn/ip.jsp?ip=" + ip);
HttpURLConnection connect = (HttpURLConnection) url
.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
byte[] b = outStream.toByteArray();
// 关闭
outStream.close();
is.close();
connect.disconnect();
String address = new String(b);
System.out.println(address);
if (address.indexOf("省") != -1) {
ipprovince = address.substring(0, address.indexOf("省") + 1);
System.out.println("省地址为:" + ipprovince);
} else if (address.indexOf("区") != -1) {
ipprovince = address.substring(0, address.indexOf("区") + 1);
System.out.println("省地址为:" + ipprovince);
} else {
ipprovince =
address.substring(0, address.indexOf("市") + 1);
System.out.println("省地址为:" + ipprovince);
}
} catch (Exception e) {
e.printStackTrace();
}
String ip = "58.214.5.162";
try {
// URL url=new
// URL("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=58.214.5.162");
URL url = new URL("http://whois.pconline.com.cn/ip.jsp?ip=" + ip);
HttpURLConnection connect = (HttpURLConnection) url
.openConnection();
InputStream is = connect.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buff = new byte[256];
int rc = 0;
while ((rc = is.read(buff, 0, 256)) > 0) {
outStream.write(buff, 0, rc);
}
byte[] b = outStream.toByteArray();
// 关闭
outStream.close();
is.close();
connect.disconnect();
String address = new String(b);
System.out.println(address);
if (address.indexOf("省") != -1) {
ipprovince = address.substring(0, address.indexOf("省") + 1);
System.out.println("省地址为:" + ipprovince);
} else if (address.indexOf("区") != -1) {
ipprovince = address.substring(0, address.indexOf("区") + 1);
System.out.println("省地址为:" + ipprovince);
} else {
ipprovince =
address.substring(0, address.indexOf("市") + 1);
System.out.println("省地址为:" + ipprovince);
}
} catch (Exception e) {
e.printStackTrace();
}
评论: 0 | 引用: 0 | 查看次数: 430
发表评论