使用Clojure 1.4(Java 7)和clj-http(0.6.0)库。 在执行get请求时,Content-Length标头会自动包含在内并设置为零。某些服务器(例如lighttpd)不喜欢这样,并使用错误请求进行响应。是否可以删除所述标题或使库默认不包含它?我在文档中找不到任何相关内容,谷歌搜索只给了我this,这对我没有帮助。
答案 0 :(得分:1)
如果我尝试:
(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错误。我尝试使用Apache HttpClient直接在Java中重现它:
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的问题提交。