【问题标题】:How can I get content length from responses header?如何从响应标头中获取内容长度?
【发布时间】:2014-05-24 09:36:01
【问题描述】:

我正在使用此代码从使用 AsyncTask 的 URL 获取数据:

 protected String doInBackground(Object... params) {
    int ResponseCode=-1;
    try {
        URL myURL=new URL("URL...");
        HttpURLConnection connection=(HttpURLConnection) myURL.openConnection();
        connection.connect();
        ResponseCode=connection.getResponseCode();

        if (ResponseCode==HttpURLConnection.HTTP_OK) {
            InputStream inputStream=connection.getInputStream();
            Reader reader=new InputStreamReader(inputStream);
            int contentLength=connection.getContentLength();
            char[] charArray=new char[contentLength];
            reader.read(charArray);
            String responseData=new String(charArray);
            Log.i("TAG", responseData);
            Log.i("TAG", ""+contentLength);
        }
        else {
            Log.i("TAG", " Unsuccessful HTTP Response Code: "+ResponseCode);
        }

    }catch (MalformedURLException e) {
            Log.i("TAG", "MalformedURLException Error: "+e.getMessage());
    } catch (IOException e) {
            Log.i("TAG", "IOException Error: "+e.getMessage());
    } catch (Exception e) {
            Log.i("TAG", "Exception Error: "+e);
    }
    return " "+ResponseCode;
}

上面的代码工作正常,我在一些项目中使用它,但是对于我正在处理的 URL,它不起作用并且总是为 Response Code,because the HTTP response of my URL doesn't contain Content-Length header, the content is chunked. 返回 -1,现在我想知道有没有有什么等效的方法吗?
在此致谢

【问题讨论】:

  • "...有没有等效的方法可以做到这一点?":很简单,没有。除非您下载整个内容并在之后检查长度,否则没有办法。唯一的可能性是,如果您控制服务器端代码并​​添加 Content-Length 响应或其他查询长度的方式。
  • 谢谢,但服务器不在我的控制范围内。

标签: java android httpconnection


【解决方案1】:

按照 http 规范,如果内容类型被分块,则可以在标题“Transfer-Encoding”之后的流上读取内容长度。它看起来像这样:

HTTP/1.1 200 OK
Date: Fri, 23 May 2014 21:17:12 GMT
Content-Type: text/plain
Transfer-Encoding: chunked

5F

这里的“5F”是从该点开始的 http 响应的十六进制长度(在本例中为 95 个字节)。

请注意,在 Header 之后和长度之前有一个换行符。

更多信息:

http协议简单解释:http://www.jmarshall.com/easy/http/#http1.1c2 RFC(官方,完整):https://www.rfc-editor.org/rfc/rfc2616

【讨论】:

    【解决方案2】:

    最后我这样解决了我的问题:

    try {
         URL blogPostURL=new URL("URL...");
         URLConnection connection=blogPostURL.openConnection();
         BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
         String responseData;
         while ((responseData = reader.readLine()) != null){ 
                 Log.i("TAG", responseData);
         }
         reader.close();
    }catch (MalformedURLException e) {
         Log.i("TAG", "MalformedURLException Error: "+e.getMessage());
    } catch (IOException e) {
         Log.i("TAG", "IOException Error: "+e.getMessage());
    } catch (Exception e) {
         Log.i("TAG", "Exception Error: "+e);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2011-07-30
    • 2016-11-04
    • 2018-06-24
    • 2020-02-24
    相关资源
    最近更新 更多