0
我正嘗試使用Java和REST連接到Web服務。這是我試過的,我得到了411錯誤。如何設置內容長度REST請求 - 對於Android
public static String getSiteToken(String host,String token) throws IOException, JSONException
{
host = host.replace("https://", "http://");
URL url = new URL(host + "/tokens");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", token);
conn.setRequestProperty("Content-Length", "57");
//conn.setFixedLengthStreamingMode(57);
conn.setRequestProperty("Connection","keep-alive");
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader rd = new BufferedReader(isr);
JSONObject json = new JSONObject(rd.readLine());
rd.close();
conn.disconnect();
return json.getString("token");
}
我還試圖「setFixedLengthStreamingMode」方法,但應用程序的代碼行後沒有響應。與REST Client for firefox連接時,一切正常。我無法弄清楚。謝謝!
您不會寫任何內容到請求的主體。在這種情況下,內容長度應該是0而不是57. – kgiannakakis
謝謝你的戰利品!它與0. –
kgiannakakis - 你應該張貼您的評論作爲答案,以便丹可以接受它。 –