java apache httpclient 内容长度问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:19:01  来源:igfitidea点击:

apache httpclient content length issue

javarestapache-httpclient-4.x

提问by Matt Broekhuis

I am trying to post some JSON to a rest service using Apache Httpclient. However, I get this error :

我正在尝试使用 Apache Httpclient 将一些 JSON 发布到休息服务。但是,我收到此错误:

    Exception in thread "main" org.apache.http.ProtocolException: Content-Length header already present


UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials(USER,
            PASS);


    HttpHost targetHost = new HttpHost("localhost", 8080, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(USER, PASS));


    HttpPost httpPost = new HttpPost(urlSuffix) {};

    JSONObject holder = new JSONObject();
    holder.put("name", "this is a folder");


    StringEntity se = new StringEntity(holder.toString());

    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.setEntity(se);



    HttpResponse resp = httpclient.execute(targetHost,httpPost);

    System.out.println("Resp->" + resp.getStatusLine().getStatusCode());


I've read its because I'm setting the content length twice already, but I'm not sure how to fix it.

我已经阅读了它,因为我已经将内容长度设置了两次,但我不确定如何修复它。

采纳答案by joelittlejohn

Your code works fine for me using HttClient 4.1.1. Which version are you using?

您的代码使用 HttClient 4.1.1 对我来说很好用。您使用的是哪个版本?

If you're certain that you aren't setting the Content-Lengthheader a second time in your code (i.e. the code posted above is exactlythe code you're running) then maybe you could try updating to the latest version of the httpclient library. You could possibly be experiencing an obscure bug like HTTPCLIENT-795.

如果您确定不会Content-Length在代码中再次设置标头(即上面发布的代码正是您正在运行的代码),那么也许您可以尝试更新到 httpclient 库的最新版本。您可能会遇到像HTTPCLIENT-795这样的晦涩错误。

回答by vdimitrov

I realise it's been awhile since this question has been asked, but here's the solution I found: In case you cannot change the client jars, the workaround that I used is:

我意识到自从提出这个问题以来已经有一段时间了,但这是我找到的解决方案:如果您无法更改客户端 jar,我使用的解决方法是:

httpClient.removeRequestInterceptorByClass(org.apache.http.protocol.RequestContent.class);

Apache's http client has a request interceptor which automatically calculates and adds the content length header, which is also done by the ws library. With the code above, this interceptor is excluded from the request processing.

Apache 的 http 客户端有一个请求拦截器,它会自动计算并添加内容长度头,这也是由 ws 库完成的。通过上面的代码,这个拦截器被排除在请求处理之外。