411 Content-Length的需要 [英] 411 Content-Length Required

查看:312
本文介绍了411 Content-Length的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做使用Android的Apache HttpClient的一个职位,但它返回错误411 Content-Length的必需。这里是code。

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

但后来我得到一个'org.apache.http.ProtocolException:Content-Length头已经present错误信息。这必须意味着HttpClient的已发送内容长度。 Unfortunatley我必须在服务器端的访问权限。任何想法,为什么它会返回这些错误?

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?

推荐答案

试试这个,

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

这篇关于411 Content-Length的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆