我正在尝试使用 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已经存在”错误。这必定意味着 HttpClient 已经发送了 Content-Length。不幸的是我无法访问服务器端。有什么想法为什么会返回这些错误吗?
尝试这个,
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)