我想使用Apache HttpClient發佈一些JSON到其他服務。不過,我得到這個錯誤:apache httpclient內容長度問題
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());
我讀過它,因爲我設置的內容長度的兩倍了,但我不知道如何解決它。
此方法現在似乎不推薦使用,任何替代方法? – voondo 2015-09-03 13:24:00