【问题标题】:Android- HttpUrlConnection addRequestProperty not workingAndroid- HttpUrlConnection addRequestProperty 不起作用
【发布时间】:2018-01-11 18:16:34
【问题描述】:

在进行更多研究后,我现在能够澄清我的问题。我有一个代码段,我知道它可以与我尝试使用的所有其他文件一起使用,除了一个文本文件。代码检查我使用 URL 和 HTTPURLConnection 发送的文件。我使用addRequestProperty("Range", "bytes=0-0") 在服务器上获取足够的文件,这样我就可以检查 LastModified 字段而无需再次下载文件。这是检查文件的代码:

  private void getFileDetails() throws IOException, ParseException {
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.addRequestProperty("Range", "bytes=0-0");
    con.connect();
    String cr = con.getHeaderField("Content-Range");
    URL url2 = new URL("url for json file");
    URL url3 = new URL("url for txt file");
    Map<String, List<String>> map = con.getHeaderFields();
    if(url.equals(url2) || url.equals(url3)) {//This is just for seeing if it added Range
        Log.i("HTTP", url.toString());
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            Log.i("HTTP", "Key : " + entry.getKey() +
                    " ,Value : " + entry.getValue());
        }
        Log.i("HTTP", "----------------------------------------------------------\n");
    }
    if (cr == null)//Fails Right here
        throw new IOException();
    length = Long.parseLong(cr.substring(cr.indexOf('/')+1, cr.length()));
    String date = con.getHeaderField("Last-Modified");
    String pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
    SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
    Date javaDate = format.parse(date);
    lastModified = javaDate.getTime();
}

这是我对有效文件字段的测试输出:

  I/HTTP: Key : null ,Value : [HTTP/1.1 206 Partial Content]
  I/HTTP: Key : Accept-Ranges ,Value : [bytes]
  I/HTTP: Key : Connection ,Value : [Keep-Alive]
  I/HTTP: Key : Content-Length ,Value : [1]
  I/HTTP: Key : Content-Range ,Value : [bytes 0-0/15431]
  I/HTTP: Key : Content-Type ,Value : [application/json]
  I/HTTP: Key : Date ,Value : [Thu, 11 Jan 2018 17:54:00 GMT]
  I/HTTP: Key : ETag ,Value : ["3c47-560c8525e5d0b"]
  I/HTTP: Key : Keep-Alive ,Value : [timeout=5, max=100]
  I/HTTP: Key : Last-Modified ,Value : [Wed, 20 Dec 2017 16:46:15 GMT]
  I/HTTP: Key : Server ,Value : [Apache/2.4.18 (Ubuntu)]
  I/HTTP: Key : X-Android-Received-Millis ,Value : [1515693240969]
  I/HTTP: Key : X-Android-Response-Source ,Value : [NETWORK 206]
  I/HTTP: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
  I/HTTP: Key : X-Android-Sent-Millis ,Value : [1515693240804]

现在这里是文本文件不起作用的结果:

 I/HTTP: Key : null ,Value : [HTTP/1.1 200 OK]
 I/HTTP: Key : Accept-Ranges ,Value : [bytes]
 I/HTTP: Key : Connection ,Value : [Keep-Alive]
 I/HTTP: Key : Content-Type ,Value : [text/plain]
 I/HTTP: Key : Date ,Value : [Thu, 11 Jan 2018 17:55:44 GMT]
 I/HTTP: Key : ETag ,Value : ["1370000-560b66148adf5-gzip"]
 I/HTTP: Key : Keep-Alive ,Value : [timeout=5, max=100]
 I/HTTP: Key : Last-Modified ,Value : [Tue, 19 Dec 2017 19:21:56 GMT]
 I/HTTP: Key : Server ,Value : [Apache/2.4.18 (Ubuntu)]
 I/HTTP: Key : Transfer-Encoding ,Value : [chunked]
 I/HTTP: Key : Vary ,Value : [Accept-Encoding]
 I/HTTP: Key : X-Android-Received-Millis ,Value : [1515693344544]
 I/HTTP: Key : X-Android-Response-Source ,Value : [NETWORK 200]
 I/HTTP: Key : X-Android-Selected-Protocol ,Value : [http/1.1]
 I/HTTP: Key : X-Android-Sent-Millis ,Value : [1515693344397]

您可以看到文本文件标题中缺少 Content-Range 字段,并且状态消息是 OK 而不是 Partial Content。我知道,因为我得到了cr == null 的真实值,这意味着它在文本文件的头文件中不存在。我不明白添加出了什么问题。我到处寻找人们遇到类似问题的地方,但整个事情都行不通。任何帮助,将不胜感激。

编辑:好的,所以真正的问题是文本文件没有长度字段。它检查 LastModified 和文件的大小以查看是否需要更新。

编辑:我现在知道出了什么问题。文本文件是分块编码的,没有Content-Length

【问题讨论】:

  • 您是否使用 ETags 来确定这些资源更改?
  • 是的,在某种程度上。我弄清楚了最近发现的问题是如何解决我遇到的问题。我会发布答案并解释更多发生的事情。

标签: java android android-studio httpurlconnection


【解决方案1】:

经过大量工作和研究,我发现我试图检查 Content-Length 标签的文件是分块编码的,这会去掉该标签。文件托管在 Apache 服务器上,自动将太大的 .txt 文件分块。解决此问题的方法是将文件扩展名从 .txt 更改为 .bin 文件。这样做给了我 Content-Length 标头,使我无需实际下载文件即可检查文件的大小。显然,自动分块编码是 HTTP 1.1 中的标准。我不确定这是否是解决此问题的最佳方法,但它在我的情况下有效。希望这对其他人有帮助。

【讨论】:

    【解决方案2】:

    你这样做是错误的。您应该使用 HEAD 请求。这样一来,除了标头外,什么都不会发送,因此不需要块编码,也不需要 Range 标头,

    【讨论】:

    • 我可以通过 HEAD 请求获取文件的大小吗?
    • 当然。这就是我建议它的原因。很奇怪。
    • 这段代码是在我进入这个项目之前编写的,我猜它工作了一段时间,但是当我试图检查 .txt 文件时它坏了。我知道我的上级将来无论如何都会将 .txt 文件更改为 bin,但我会检查 HEAD 请求,看看我是否可以更改现有代码。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 2016-06-29
    相关资源
    最近更新 更多