dd_1d 2015-12-02 04:01 采纳率: 10%
浏览 1751

HttpURLConnection.getInputStream获取长度为-1

百度得知Android2.2以上使用getInputStream,需要加上

 hc.setRequestProperty("Accept-Encoding", "identity");

可是试了下还是不行。。。

            URL url = new URL(s);
            HttpURLConnection hc = (HttpURLConnection)url.openConnection();

            hc.setRequestMethod("GET");
            hc.setDoInput(true);
            hc.setUseCaches(false);
            hc.setInstanceFollowRedirects(true);
            hc.setRequestProperty("Accept-Encoding", "identity"); //加上
            hc.setRequestProperty("Host", "api.yeelink.net");
            hc.setRequestProperty("U-ApiKey", "4d968660ad88e84f2c66ee5723e60cf8");
            hc.setRequestProperty("Content-Length", "0");
            hc.setRequestProperty("Connection", "close");

            hc.connect(); 

            int streamLength = hc.getContentLength();
  • 写回答

1条回答 默认 最新

  • 上海好程序员 2020-06-14 11:37
    关注

    安卓bug,spring没有此问题

    评论

报告相同问题?