当前位置: 首页 > 知识库问答 >
问题:

411内容-所需长度

瞿博易
2023-03-14

我试图使用Android Apache HttpClient做一个POST,但它返回错误411内容长度必需。这是代码。

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

我已经尝试添加行:-

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

但我得到了一个“组织”。阿帕奇。http。ProtocolException:内容长度标头已出现“错误”。这必须意味着HttpClient已经在发送内容长度。不幸的是,我无法访问服务器端。知道为什么会返回这些错误吗?

共有1个答案

戚飞虎
2023-03-14

试试这个,

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);
 类似资料:
  • 我正在使用HttpClient 4.3。3.我的任务是将文件上传到服务器上。以下是我的代码: 当我运行此代码时,它成功地连接到服务器。但响应的输出是 当我使用firebug进行调试时,我看到以下请求头。 响应头视图源 缓存控制无存储,无缓存 连接保持活力 日期2014年5月22日星期四08:58:02 GMT 保持活动超时=30 服务器***** 设置Cookie****路径=/ 转移编码分块 请

  • 问题内容: 我使用node.js并在一个小项目中表达。我将响应头设置为打击: 我可以使用打印数据的长度为317。 但是在浏览器的控制台上,我得到了这些: 连接:keep-alive 内容编码:gzip 内容类型:文本/纯文本; charset = utf- 8日期 :2013年6月1日星期六,08 :21:59 GMT 传输编码:块状 变化:接受编码 X-Powered-作者:快递 那么,为什么

  • 问题内容: 我正在尝试使用连接服务器,但是PUT方法存在问题。 我需要发送一个包含 1500个字符 (或更多)的字符串,但是在这种情况下,服务器会产生超时并返回 500个服务器内部错误 。 如果我发送的字符串少于 1400个字符 ,则没有问题,服务器返回 OK 。 我的代码如下: // ----------------------- 我尝试添加线 但是服务器的答案还是错误的。 更新: 我可以发现问

  • 我的HTTP头有问题。我需要发送一个带有空正文和标题“content-length:0”的post-request。但这似乎是不可能的,因为我有一个例外: --org.apache.http.ProtocolException:内容长度头已存在-- 编辑: 用@Beno Arakelyan的答案解决了这个问题。

  • HTTP 411 Length Required客户端错误响应代码指示服务器在没有定义的Content-Length头部的情况下拒绝接受请求。 请注意,根据规范,在一系列块中发送数据时,Content-Length头部将被忽略,并且在每个块的开始处,您需要以十六进制格式添加当前块的长度。查看Transfer-Encoding更多细节。 状态 411 Length Required 规范 规范 标题

  • 问题内容: 将JSON数据从JSP传递到ResponseBody中的控制器时出错。 Ajax电话: 控制器: AppConfig.java @豆 请帮助我摆脱困境。我正在使用Spring 4,Jakson 2.3.0 如果我尝试POST请求,它将给出:org.springframework.web.HttpRequestMethodNotSupportedException:请求方法’POST’不