Android-HttpUrlConnection addRequestProperty无效

时间:2018-01-11 18:16:35

标签: java android android-studio httpurlconnection

经过更多的研究,我现在能够澄清我的问题。我有一个代码段,我知道除了一个文本文件,我尝试使用的每个其他文件。代码使用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

2 个答案:

答案 0 :(得分:0)

经过大量的工作和研究,我发现我试图检查内容长度标签的文件是分块编码的,它取消了该标签。 Apache服务器上的文件托管在太大的自动块.txt文件上。解决此问题的方法是将文件扩展名从.txt更改为.bin文件。这样做给了我Content-Length标头,允许我检查文件的大小而不必实际下载文件。显然,自动分块编码是HTTP 1.1中的标准编码。我不确定这是否是解决此问题的最佳方式,但它在我的案例中有效。希望这有助于其他人。

答案 1 :(得分:0)

你这样做是错误的。您应该使用HEAD请求。这样,除了标题之外,发送的是nothng,所以没有块编码,也不需要Range头,