3
我正在嘗試使用Android Apache HttpClient執行POST,但它返回錯誤411 Content-Length必需。這是代碼。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頭已經存在' 錯誤信息。這意味着HttpClient已經發送了Content-Length。 Unfortunatley我無法訪問服務器端。任何想法爲什麼它會返回這些錯誤?
你試過發佈此使用HTTP,而不是HTTPS和運行Wireshark的,看看到底就是BEING產生什麼電腦?你使用的是什麼版本的Android HTTP客戶端? – HeatfanJohn 2013-03-21 15:58:13
我將它發送到HTTP地址並運行wireshark。它沒有在Wireshark中作爲一個帖子出現,而是作爲[重新組裝的PDU的TCP段],不知道這意味着什麼。這是捕獲包。 – user1240059 2013-03-21 19:14:31
TRSNIFF data uBôÿk=æ!·,+ $ w72l EØ[email protected]@?ãμÈP-rX³Á7-Ph「POST ************ HTTP/1.1 Content-Type:application/xml; charset = UTF-8 授權:基本版*************** 內容長度:103 主持人:************* Connection:Keep-Alive User-Agent:Apache-HttpClient/UNAVAILABLE(java 1.4) {「grant_type」:「authorization_code」,「scope」:「https:\/\/uri.paypal.com \/services \/paypalhere「,」code「:」***********「} – user1240059 2013-03-21 19:15:45