JSP获取IP所属城市 http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18如何取到response回来一串字符串中的城市 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你返回的字符串是什么样子的?是json格式的、还是就是一长串字符串? public class URLT { public static void main(String[] args) throws IOException { String strURL = "http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18"; System.out.println(getURLContents(strURL)); } public static String getURLContents(String strURL) { StringBuffer sb = new StringBuffer(); try { URL url = new URL(strURL); URLConnection connection = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection) connection; System .setProperty("sun.net.client.defaultConnectTimeout", "150000"); System.setProperty("sun.net.client.defaultReadTimeout", "150000"); httpConn.setRequestMethod("GET");// 设置请求为POST方法 connection.setDoOutput(true);// 可以输出 InputStreamReader isr = new InputStreamReader(httpConn .getInputStream(), "GBK"); BufferedReader br = new BufferedReader(isr); String temp; while ((temp = br.readLine()) != null) { temp = temp.trim(); if (temp != null && temp.length() > 0) { sb.append(temp); } } br.close(); isr.close(); } catch (Exception e) { System.out.println("Error 1" + e.getMessage()); } return sb.toString(); }}运行结果:您查询的IP是:<span class='orange'>218.56.35.18</span> 来自:山东省烟台市 联通截取就可以了、 java.net.UnknownHostException: www.ip.cn为什么啊 请教一个jquery获取的对象的问题 新手求助,onlick调用javascript函数没反应,求帮我看看怎么回事 有关tomcat5.5的问题 放到空间上的网站IE能正常访问,但是谷歌 火狐浏览器 就直接显示源码了。 关于Spring控制事务的问题,谢谢帮忙!! 一个很简单很小的问题…… 求救:jsp中如何實現表dbf中數據導入postgresql數據庫的表中 如何成为好的程序员? 新手的JSP问题 简单邮件系统,HIbernate用户和邮件关系映射问题 jsp struts2一次上传多张图片 ajax局部有刷新问题
public static void main(String[] args) throws IOException {
String strURL = "http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18";
System.out.println(getURLContents(strURL));
} public static String getURLContents(String strURL) {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(strURL);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
System
.setProperty("sun.net.client.defaultConnectTimeout",
"150000");
System.setProperty("sun.net.client.defaultReadTimeout", "150000");
httpConn.setRequestMethod("GET");// 设置请求为POST方法
connection.setDoOutput(true);// 可以输出
InputStreamReader isr = new InputStreamReader(httpConn
.getInputStream(), "GBK");
BufferedReader br = new BufferedReader(isr);
String temp;
while ((temp = br.readLine()) != null) {
temp = temp.trim();
if (temp != null && temp.length() > 0) {
sb.append(temp);
}
}
br.close();
isr.close();
} catch (Exception e) {
System.out.println("Error 1" + e.getMessage());
}
return sb.toString();
}
}运行结果:
您查询的IP是:<span class='orange'>218.56.35.18</span> 来自:山东省烟台市 联通截取就可以了、
为什么啊