http://www.ip.cn/getip2.php?action=queryip&ip_url=218.56.35.18
如何取到response回来一串字符串中的城市

解决方案 »

  1.   

    你返回的字符串是什么样子的?是json格式的、还是就是一长串字符串?
      

  2.   

    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>  来自:山东省烟台市 联通截取就可以了、
      

  3.   

    java.net.UnknownHostException: www.ip.cn
    为什么啊