在执行get请求时,会自动包含Content-Length报头并将其设置为零。是否可以删除上述标头,或者默认情况下使库不包含它?
发布于 2012-12-10 01:14:26
如果我尝试:
(client/get "http://thepiratebay.se" {:debug true})我得到了:
Request: nil
{:scheme :http,
:http-url "http://thepiratebay.se",
:request-method :get,
:query-string nil,
:uri "",
:server-name "thepiratebay.se",
:headers {"accept-encoding" "gzip, deflate"},
:debug true,
:body-type nil,
:server-port nil,
:body nil,
:user-info nil}
HttpRequest:
{:requestLine #<BasicRequestLine GET http://thepiratebay.se HTTP/1.1>,
:protocolVersion #<HttpVersion HTTP/1.1>,
:params
#<BasicHttpParams org.apache.http.params.BasicHttpParams@5b14a306>,
:method "GET",
:entity nil,
:class
clj_http.core.proxy$org.apache.http.client.methods.HttpEntityEnclosingRequestBase$0,
:allHeaders
[#<BasicHeader Connection: close>,
#<BasicHeader accept-encoding: gzip, deflate>],
:aborted false,
:URI #<URI http://thepiratebay.se>} 这将产生一个400错误。我尝试用Java语言直接使用Apache HttpClient重现它:
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.Header;
public class Get {
public static void main(String args[]) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(args[0]);
httpget.addHeader("Connection", "close");
httpget.addHeader("accept-encoding", "gzip, deflate");
Header[] headers = httpget.getAllHeaders();
for (Header h : headers) {
System.out.println(h.getName() + ", " + h.getValue());
}
System.out.println();
HttpResponse response = httpclient.execute(httpget);
System.out.println(response);
}
}但是,这可以很好地工作。我的猜测是,在调用HttpClient之前,clj-http正在做一些强制在响应中使用空体的操作,因此HttpClient将头部Content-Length设置为0。如果你看一下源代码,就会发现这个头不是由clj-http设置的。我会把这个作为一个问题提交给clj-http。
https://stackoverflow.com/questions/13783892
复制相似问题