yanzhenjie / Kalle

:apple: Http client for Android platform.
https://yanzhenjie.com/Kalle
Apache License 2.0
592 stars 98 forks source link

HEAD 请求方式无法返回 #83

Open xiexindev opened 5 years ago

xiexindev commented 5 years ago
 implementation 'com.yanzhenjie:kalle:0.1.7'
 SimpleResponse<String, String> response =Kalle
        .head("http://www.baidu.com")
        .perform(String.class, String.class);
 System.out.println(response.headers());

无论是同步还是异步方式, 都不会得到response. Debug到源码时, 发现BasicWorker#line 79 获取body的时候就没了.

 Headers headers = response.headers();
            byte[] body = {};
            if (code != 204) {
                //这里得不到body
                body = response.body().byteArray();
            }

CatLog都已经打印了响应头:

 Print Request: http://www.baidu.com/.
    Method: HEAD.
    Accept: */*.
    Accept-Encoding: gzip, deflate.
    Accept-Language: zh-CN,zh.
    Connection: keep-alive.
    Content-Type: application/x-www-form-urlencoded.
    Host: www.baidu.com.
    User-Agent: Mozilla/5.0 (Linux; U; Android 6.0; en; PA61 Api/MRA58L) AppleWebKit/534.30 (KHTML, like Gecko) Version/5.0 Mobile Safari/534.30. 
    Print Response: http://www.baidu.com/.
    Code: 200
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform.
    Connection: keep-alive.
    Content-Encoding: gzip.
    Content-Type: text/html.
    Date: Wed, 30 Oct 2019 08:41:20 GMT.
    Last-Modified: Mon, 13 Jun 2016 02:50:13 GMT.
    Pragma: no-cache.
    Server: bfe/1.0.8.18.
    X-Android-Received-Millis: 1572424878750.
    X-Android-Response-Source: NETWORK 200.
    X-Android-Selected-Protocol: http/1.1.
    X-Android-Sent-Millis: 1572424878702.

我猜测问题出在这个判断这里, HEAD请求方式code会等于200但是它是没有响应body的.

if (code != 204) {
                //这里得不到body
                body = response.body().byteArray();
            }
xiexindev commented 5 years ago

也没有抛异常, 总之就是执行到这一步时就好像直接中断了一样 实际上是进入了死循环了