2011-09-26 73 views
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连接时,一切正常。我无法弄清楚。谢谢!

+1

您不会写任何内容到请求的主体。在这种情况下,内容长度应该是0而不是57. – kgiannakakis

+0

谢谢你的战利品!它与0. –

+1

kgiannakakis - 你应该张贴您的评论作为答案,以便丹可以接受它。 –

回答

0

你没有写任何东西给请求的主体。在这种情况下,内容长度应该是0而不是57

相关问题