Android Apache HttpClient を使用して POST を実行しようとしていますが、エラー 411 Content-Length Required が返されます。これがコードです。
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()));
しかし、代わりに 'org.apache.http.ProtocolException: Content-Length header already present' エラーが発生します。これは、HttpClient が既に Content-Length を送信していることを意味する必要があります。残念ながら、サーバー側にはアクセスできません。これらのエラーが返される理由はありますか?