当前位置: 代码迷 >> Java Web开发 >> JSP获取IP所属城市,该如何处理
  详细解决方案

JSP获取IP所属城市,该如何处理

热度:1161   发布时间:2013-02-25 21:22:46.0
JSP获取IP所属城市
http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18
如何取到response回来一串字符串中的城市

------解决方案--------------------------------------------------------
你返回的字符串是什么样子的?是json格式的、还是就是一长串字符串?
------解决方案--------------------------------------------------------
就把你传的值给获取呀
------解决方案--------------------------------------------------------
关注,这个问题有时会碰到
------解决方案--------------------------------------------------------
Java code
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();    }}
  相关解决方案