gpt4 book ai didi

http - get 请求中的零内容长度从 lighttpd (clj-http) 获取 400 Bad Request

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:00 24 4
gpt4 key购买 nike

使用 Clojure 1.4 (Java 7) 和 clj-http (0.6.0) 库。在执行获取请求时,Content-Length header 会自动包含并设置为零。一些服务器(例如 lighttpd)不喜欢这样并以 Bad Request 响应。是否可以删除所述 header 或使库默认不包含它?我在文档中找不到任何相关内容,谷歌搜索只给了我 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 中重现它,直接使用 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 将 header Content-Length 设置为 0。如果您查看源代码,则 header 不是由 clj-http 设置的。我会将此作为 clj-http 的问题归档。

https://github.com/dakrone/clj-http/issues

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com