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

Java中URL内容的读取——重定向问题

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

    当我执行下面的Java程序时,有时会得到一个 空响应 有时我会 真实(重定向)内容 .

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    URL url = new URL( "https://stackoverflow.com/questions/84629" );
    IOUtils.copy( url.openStream(), output );
    System.out.println( output.toString() );
    

    网址 https://stackoverflow.com/questions/84629 是重定向到 What's your favorite "programmer" cartoon? .

    我仔细研究了其他问题,并尝试使用建议的 HttpUrlConnection 但结果是一样的。响应代码总是200,但有时有正确的HTML输出,有时只是一个空字符串。

    你能解释一下这里发生了什么吗?


    编辑

    以下是没有Apache Commons的代码:

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    URL url = new URI( "https://stackoverflow.com/questions/84629" ).toURL();
    InputStream openStream = url.openStream();
    byte[] buffer = new byte[ 1024 ];
    int size = 0;
    while( (size = openStream.read( buffer ) ) != -1 ) {
        output.write( buffer, 0, size );
    }
    System.out.println( output.toString() );
    

    我使用的是WindowsXP和Java1.60Y17。


    我用wireshark捕捉到了流量:

    GET /questions/84629 HTTP/1.1
    User-Agent: Java/1.6.0_17
    Host: stackoverflow.com
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive
    
    HTTP/1.1 200 OK
    Cache-Control: private
    Server: Microsoft-IIS/7.0
    Date: Tue, 10 Nov 2009 22:42:42 GMT
    Content-Length: 0
    
    3 回复  |  直到 13 年前
        1
  •  2
  •   ChadNC    15 年前

    我以前在创建类似于您发布的示例的URL时遇到过问题,我发现解决该问题的最佳方法是创建一个 URI 然后像这样创建URL

    URL url = uri.toUrl()
    

    我不完全确定您的问题是否与我的问题相同,但是当我创建类似于您的示例中的URL时,每次都没有正确编码,这在某些情况下会导致错误,但是使用一个URI为我修复了它。

        2
  •  0
  •   amischiefr    15 年前

    我也有类似的问题,最后不得不自己使用httpurlconnection。我不记得你描述的httpurl连接有问题。这里的代码完成了任务(打印到屏幕上,但您可以根据需要更改代码)

    public void connect() {
        try {
            String url = "http://www.stackoverflow.com",
                   proxy = "proxy.mydomain.com",
                   port = "8080";
            URL server = new URL(url);
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
            connection.connect();
            InputStream in = connection.getInputStream();
            readResponse(in);
        } catch(Exception e) {
            e.printStackTrace();
        }
    
    }
    public void readResponse(InputStream is) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result = bis.read();
        while(result != -1) {
          byte b = (byte)result;
          buf.write(b);
          result = bis.read();
        }        
        System.out.println(buf.toString());
    }
    
        3
  •  0
  •   tangens    15 年前

    我觉得这就像@carlsmofricz说的:“你可能碰到了垃圾邮件过滤器之类的东西。”

    推荐文章