2013-05-14 3 views
0

사용자 로그인을위한 게시물 요청을 만들어야합니다. 나는 콘텐츠 길이 헤더를 설정하고하지만 난 요청을 게시 할 때 org.apache.http.client.HttpResponseException 제공 : 필수 길이를android - http post content-length (json post)

난 콘텐츠 길이 헤더를 설정

 DefaultHttpClient httpclient = new DefaultHttpClient(); 

     JSONObject data = new JSONObject(); 
     data.put("UserName", username); 
     data.put("Password", password); 
     data.put("RememberMe", false); 

     HttpPost httpost = new HttpPost(LOGIN_PATH); 
     StringEntity se = new StringEntity(data.toString()); 
     httpost.setEntity(se); 

     httpost.setHeader("Accept", "application/json"); 
     httpost.setHeader("Content-Type", "application/json"); 
     httpost.setHeader("Content-Length",""+se.getContentLength()); 

     ResponseHandler responseHandler = new BasicResponseHandler(); 
     return httpclient.execute(httpost, responseHandler); 

내 코드는 준다 'Content-Length header already present'이고, content-lenght header를 제거하면 'Length is required'가됩니다.

고마워요.

+0

에 열쇠로 Content-Length 대신 Content-Lenght (길이 맞춤법 실수)를 통과하기 때문에

httpost.setHeader("Content-Length",""+se.getContentLength()); 

에 . 감사합니다 – dracula

답변

2

변화

httpost.setHeader("Content-Lenght",""+se.getContentLength()); 

현재 당신이 HttpURLConnection의 inntead DefaultHttpClient과의 HttpPost로 해결 setHeader 방법

+0

죄송합니다. 나는 그것을 놓쳤다. Content-Length 헤더를 설정하면 'Content-Length header already present'가 표시되고, content-lenght 헤더를 제거하면 'Length is required'가 부여됩니다. – dracula

+0

@dracula, 이미 존재하는 Content-Length 헤더를 해결할 수 있었습니까? 문제? 나는 같은 것을 때리고있다. 나 좀 도와 줘. – Gopinath

+0

@Gopinath, 네, HttpURLConnection으로 해결하고 연결의 RequestProperties를 설정했습니다. – dracula