使用clojure 1.4(java 7)和clj http(0.6.0)库。
执行get请求时,内容长度头将自动包含并设置为零。一些服务器(例如lighttpd)不喜欢这样,并用错误的请求响应。是否可以删除所述头或使库默认不包含它?我在文档中找不到任何相关信息,google只给了我this,这并没有真正的帮助。

最佳答案

如果我尝试:

(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直接使用ApacheHttpClient复制它:
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将头内容长度设置为0。如果查看源代码,则CLJHTTP不会设置头。我将此作为clj http的问题提交。
https://github.com/dakrone/clj-http/issues

关于http - 获取请求中的零内容长度从lighttpd(clj-http)获取400错误请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13783892/

10-11 21:50