【问题标题】:Java HTTP Request : get content sizeJava HTTP 请求:获取内容大小
【发布时间】:2015-02-05 13:44:12
【问题描述】:

我想知道是否可以通过 http 请求获取网页的大小。

我用它来获得 oracle 页面长度:

URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
List<String> get = yc.getHeaderFields().get("content-Length");

但是当我将它用于谷歌页面时,标题中没有内容长度。

【问题讨论】:

  • 您必须使用正确的大写或小写。字段名称是“内容长度”而不是“内容长度”。您还可以使用URLConnection 对象的getContentLength() 方法。任何需要发送 HTTP 正文的应用程序都应使用此字段,而 google 就是这样做的。
  • @Jean-BaptisteYunès 这应该是一个答案。

标签: java http-headers content-length


【解决方案1】:

您必须使用正确的大写或小写。字段名称是 Content-Length 而不是 content-Length。您还可以使用URLConnection objectgetContentLength() 方法。任何需要发送 HTTP 正文的应用程序都应使用此字段,而 google 就是这样做的。

请注意 Google 使用安全连接。

【讨论】:

    【解决方案2】:

    Content-Length 仅在响应不是 chunked 时才适用。 一般静态内容不会被分块,但在很多情况下动态内容不会被分块。

    对于 www.oracle.com,您将被重定向到 www.oracle.com/index.html,这是静态内容并提供 Content-Length。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 2023-03-10
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      相关资源
      最近更新 更多