代码之家  ›  专栏  ›  技术社区  ›  Pierre

下载网页。wget正常,java失败

  •  2
  • Pierre  · 技术社区  · 15 年前

    我正在尝试下载以下页面: http://structureddata.wikispaces.com/Test

    wget公司 如果没有任何选项,则失败:

    wget "http://structureddata.wikispaces.com/Test"
    (...) connect to session.wikispaces.com insecurely, use `--no-check-certificate'
    

    如果没有支票证书,它就可以工作了

    wget --no-check-certificate "http://structureddata.wikispaces.com/Test"
    grep Hello Test
     Hello World
    

    import java.net.*;
    import java.io.*;
    public class Test
            {
            public static void main(String args[])
                    {
                    int c;
                    try
                            {
                            InputStream in=new URL("http://structureddata.wikispaces.com/Test").openStream();
                            while((c=in.read())!=-1) System.out.print((char)c);
                            in.close();
                            }
                    catch(Throwable err)
                            {
                            err.printStackTrace();
                            }
                    }
            }
    

    不返回任何内容

    非常感谢,

    皮耶尔

    3 回复  |  直到 14 年前
        1
  •  3
  •   Dave Costa    15 年前

    javaurl接口的级别相当低;它不会自动执行诸如跟随重定向之类的操作。上面的代码没有要打印的内容,因为没有内容。

      URL url = new URL("http://structureddata.wikispaces.com/Test");
    
      URLConnection urlConnection = url.openConnection();
      Map<String, List<String>> headers = urlConnection.getHeaderFields();
      Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
      for (Map.Entry<String, List<String>> entry : entrySet) {
        String headerName = entry.getKey();
        System.out.println("Header Name:" + headerName);
        List<String> headerValues = entry.getValue();
        for (String value : headerValues) {
          System.out.print("Header value:" + value);
        }
        System.out.println();
        System.out.println();
      }
    

    HTTPClient 这将为你处理更多的协议。

    (信用证到期日:复制上述代码自 here .)

        2
  •  2
  •   Jon Freedman    15 年前

    你可以看看 commons-httpclient ,此代码返回的页面没有问题

    final HttpClient client = new HttpClient();
    final GetMethod method = new GetMethod("http://structureddata.wikispaces.com/Test");
    try {
        if (HttpStatus.SC_OK == client.executeMethod(method)) {
            System.out.println(IOUtils.toString(method.getResponseBodyAsStream()));
        } else {
            throw new IOException("Unable to load page, error " + method.getStatusLine());
        }
    } finally {
        method.releaseConnection();
    }
    
        3
  •  2
  •   BalusC    15 年前

    302 将响应重定向到 https http 目标是 https协议 URLConnection 不会自动遵循重定向(但是,当目标使用相同的方案时,会这样做)。

    经过一番观察,我得出的结论是 请求一些身份验证令牌,这些令牌又被重定向到 http协议 http协议 https协议 然后 http协议

    下面的工作在这里。

    public static void main(String... args) throws Exception {
        // First request.
        URLConnection connection = new URL("http://structureddata.wikispaces.com/Test").openConnection();
    
        // Go to the redirected https page to obtain authentication token.
        connection = new URL(connection.getHeaderField("location")).openConnection();
    
        // Re-request the http page with the authentication token.
        connection = new URL(connection.getHeaderField("location")).openConnection();
    
        // Show page.
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            for (String line; ((line = reader.readLine()) != null);) {
                System.out.println(line);
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException ignore) {}
        }
    }
    

    Commons HttpComponents Client 是更好的工作工具。