2012-12-09 72 views
1

使用Clojure 1.4(Java 7)和clj-http(0.6.0)庫。 當執行獲取請求時,Content-Length標題會自動包含並設置爲零。有些服務器(例如lighttpd)不喜歡這種服務器,並以錯誤請求進行響應。是否有可能刪除所述標題或使庫默認不包含它?我找不到任何與文檔相關的內容,並且使用Google搜索功能只給我提供了this,這並沒有什麼幫助。獲取請求中的零內容長度獲取400來自lighttpd的錯誤請求(clj-http)

+0

我不能使用Clojure 1.4和CLJ-HTTP 0.6.0對幾個網站我試過了,從這個名單重現該問題:http://redmine.lighttpd.net/projects/1/wiki/ PoweredByLighttpd你知道有哪些網站可靠地迴應壞請求? –

+0

請嘗試http://thepiratebay.se。在java.net.URL上使用openConnection執行獲取請求時,我無法重現該問題。在檢查頭文件時,我可以看到的唯一區別是包含來自clj-http的零Content-Length。 – kliron

回答

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 [email protected]>, 
: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://github.com/dakrone/clj-http/issues

+0

好,我只是把它作爲一個問題提交。 – kliron

相關問題