apache httpclient 内容长度问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到这个问题已经有一段时间了,但这是我找到的解决方案:
如果您无法更改客户端 jar,我使用的解决方法是:
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:
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.
使用 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.