Java HttpURLConnection仅适用于某些站点

时间:2015-08-20 17:47:17

标签: java httpurlconnection

...
发生了一件奇怪的事。我无法显示结果,长度为-1。但是当我使用其他网址时。例如,pipe the output。有用。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你能这样试试吗?地址应设置为您要阅读的网站

    URL page = new URL(address);
    StringBuffer text = new StringBuffer();
    HttpURLConnection conn = (HttpURLConnection) page.openConnection();
    conn.connect();
    InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
    BufferedReader buff = new BufferedReader(in);
    box.setText("Getting data ...");
    String line;
    do {
      line = buff.readLine();
      text.append(line + "\n");
    } while (line != null);
    final String result = new String(text.toString());