2011-03-29 7 views
3

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()); 

이미 두 번 콘텐츠 길이를 설정하고 있습니다 때문에 나는 그것을 읽었습니다,하지만 난 그것을 해결하는 방법을 모르겠어요.

답변

2

코드가 HttClient 4.1.1을 사용하여 정상적으로 작동합니다. 어떤 버전을 사용하고 있습니까?

당신은 당신의 코드에서 Content-Length 헤더를 두 번째 시간을 설정하지 않았는지 경우

어쩌면 당신이 최신으로 업데이트 시도 할 수 있습니다 (위의 게시 된 코드는 정확히 실행중인 코드입니다 IE) 버전의 httpclient 라이브러리. 아마도 HTTPCLIENT-795과 같은 애매한 버그가 발생할 수 있습니다.

4

나는 그것이이 질문은 요청을받은 이후 잠시왔다,하지만 여기 내가 찾은 솔루션의 실현 :

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

아파치 HTTP 클라이언트 : 경우 당신은 내가 사용하는 해결 방법은 클라이언트 항아리를 변경할 수 없습니다 ws 라이브러리에 의해 수행되는 내용 길이 헤더를 자동으로 계산하고 추가하는 요청 인터셉터가 있습니다. 위의 코드에서이 인터셉터는 요청 처리에서 제외됩니다.

+1

이 방법은 현재 사용되지 않습니다. – voondo

관련 문제