4

I am trying to do a POST using the Android Apache HttpClient but it is returning error 411 Content-Length Required. Here is the code.

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost request = new HttpPost("https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice");
            request.addHeader("Authorization","Basic "+ Base64.encodeToString((appId+":"+ appSecret).getBytes(),Base64.DEFAULT)); 



            List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
            postParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));                  
            postParameters.add(new BasicNameValuePair("code", code));                  
            postParameters.add(new BasicNameValuePair("scope", "https://uri.paypal.com/services/paypalhere"));                  

                UrlEncodedFormEntity entity;
                entity = new UrlEncodedFormEntity(postParameters);

                request.setEntity(entity);
                HttpResponse response = httpClient.execute(request);

                Log.d("HTTPStatus",response.getStatusLine().toString());

                InputStream bufferedReader =         
                        response.getEntity().getContent();   
                StringBuffer stringBuffer = new StringBuffer("");   
                byte[] line = new byte[1024];   
                while (bufferedReader.read(line) > 0) {    
                    stringBuffer.append(new String(line));    
                    }   
                bufferedReader.close();

                Log.d("str",stringBuffer.toString());

I have tried adding the line:-

                request.addHeader("Content-Length",Long.toString(entity.getContentLength()));

But then I get a 'org.apache.http.ProtocolException: Content-Length header already present' error instead. This must mean that the HttpClient is already sending the Content-Length. Unfortunatley I have no access to the server side. Any ideas why it would be returning these errors?

user1240059
  • 95
  • 2
  • 9
  • Have you tried to post this using HTTP instead of HTTPS and to a PC running Wireshark to see exactly what's being generated? Also what version of the Android HTTP client are you using? – HeatfanJohn Mar 21 '13 at 15:58
  • I sent it to a HTTP address and ran wireshark. It did not come up as a post in Wireshark but as a [TCP segment of resassembled PDU], don't know what that means. Here is the capture packet. – user1240059 Mar 21 '13 at 19:14
  • TRSNIFF data uB ô ÿk= æ  !·,+$w72l E ØGQ@ @?ãÀ¨ ­ÂCyÈ P—rX³Á7 -Ph„ POST ************ HTTP/1.1 Content-Type: application/xml;charset=UTF-8 Authorization: Basic *************** Content-Length: 103 Host: ************* Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) {"grant_type":"authorization_code","scope":"https:\/\/uri.paypal.com\/services\/paypalhere","code":"***********"} – user1240059 Mar 21 '13 at 19:15
  • Please refer ansver here, it seems you are having the same problem http://stackoverflow.com/questions/6857796/content-length-is-null-for-entitytemplate – Artem Zelinskiy May 15 '13 at 13:04

1 Answers1

3

Try this,

HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost("https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice");
        request.setHeader("Content-type", "application/json");
        request.setHeader("Accept", "application/json");
        request.addHeader("Authorization", "Basic " + Base64.encodeToString((appId + ":" + appSecret).getBytes(), Base64.DEFAULT));


        JSONObject obj = new JSONObject();
        obj.put("grant_type", "authorization_code");
        obj.put("code", code);
        obj.put("scope", "https://uri.paypal.com/services/paypalhere");    
        request.setEntity(new StringEntity(obj.toString(), "UTF-8"));         

        HttpResponse response = httpClient.execute(request);
Talha
  • 12,673
  • 5
  • 49
  • 68