开发者

How to set Content Length REST Request - for Android

开发者 https://www.devze.com 2023-04-07 21:22 出处:网络
I\'m trying to connect to a webservice using Jav开发者_Python百科a and REST. This is what I\'ve tried, and I get a 411 error.

I'm trying to connect to a webservice using Jav开发者_Python百科a and REST. This is what I've tried, and I get a 411 error.

   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");


}

I also tried " setFixedLengthStreamingMode " method, but the application wasn't responding after that line of code. Everything works fine when connecting with REST Client for firefox. I can't figure it out. Thanks!


You aren't writing anything to the body of the request. In that case the content length should be 0 and not 57

0

精彩评论

暂无评论...
验证码 换一张
取 消