3

我正在尝试使用 java 中的 HttpsURLConnection 从远程服务器下载 xml.gz 文件,但我得到一个空响应。这是我的代码示例:

URL server = new URL("https://www.myurl.com/path/sample_file.xml.gz");
HttpsURLConnection connection = (HttpsURLConnection)server.openConnection();
connection.connect();

当我尝试从连接中获取 InputStream 时,它是空的。(如果我尝试 connection.getInputStream().read() 我得到-1)我期望的文件大约是 50MB。

为了测试我的理智,我还尝试在浏览器中输入完全相同的 url,它确实返回了我需要的文件。我错过了什么吗?我是否必须在连接中设置某种参数?非常感谢任何帮助/指导。

4

3 回答 3

2

Is any exception being logged? Is the website presenting a self-signed SSL certificate, or one that is not signed by a CA? There are several reasons why it might work fine in your browser (the browser might have been told to accept self-signed certs from that domain) and not in your code.

What are the results of using curl or wget to fetch the URL?

The fact that the InputStream is empty / result from the InputStream.read() == -1 implies that there is nothing in the stream to read, meaning that the stream was not able to even be set up properly.

Update: See this page for some info on how you can deal with invalid/self-signed certificates in your connection code. Or, if the site is presenting a certificate but it is invalid, you can import it into the keystore of the server to tell Java to trust the certificate. See this page for more info.

于 2008-10-27T17:37:32.063 回答
2
  1. Verify the response code is 200
  2. Check that connection.contentType to verify the content type is recognized
  3. You may need to add a Content-Handler for the GZ mime type, which I can't recall off the top of my head.

After the comment describing the response code as 3xx,

  1. Set 'connection.setFollowRedirects(true)'

Should fix it.

于 2008-10-27T18:03:22.197 回答
1

原来下载不起作用,因为远程服务器正在将我重定向到一个新的 url 来下载文件。即使设置了 connection.setFollowRedirects(true),我仍然必须手动为重定向的 URL 设置新连接,如下所示:

if (connection.getResponseCode() == 302 && connection.getHeaderField("location") != null){
            URL server2 = new URL(connection.getHeaderField("location"));
            HttpURLConnection connection2 = (HttpURLConnection)server2.openConnection();
            connection2.connect();
            InputStream in = connection2.getInputStream();
}

之后,我能够从输入流中检索文件。感谢您的所有帮助!

于 2008-10-28T18:27:08.857 回答