apache httpclient 内容长度问题

发布于 2024-10-28 07:23:26 字数 1184 浏览 1 评论 0原文

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

    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 am trying to post some JSON to a rest service using Apache Httpclient. However, I get this error :

    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-11-04 07:23:26

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

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

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

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:

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.

童话 2024-11-04 07:23:26

使用 HttClient 4.1.1,您的代码对我来说工作正常。您使用的是哪个版本?

如果您确定没有在代码中再次设置 Content-Length 标头(即上面发布的代码完全正是您正在运行的代码) )那么也许您可以尝试更新到最新版本的 httpclient 库。您可能遇到了一个不起眼的错误,例如 HTTPCLIENT-795

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

If you're certain that you aren't setting the Content-Length header a second time in your code (i.e. the code posted above is exactly the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文