我正在尝试使用 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());
我已经阅读了它,因为我已经设置了两次内容长度,但我不确定如何修复它。