我需要为用户登录创建一个发布请求。我正在设置 Content-Length 标头,但是当我发布请求时,它会给出org.apache.http.client.HttpResponseException: Length Required
我的代码
DefaultHttpClient httpclient = new DefaultHttpClient();
JSONObject data = new JSONObject();
data.put("UserName", username);
data.put("Password", password);
data.put("RememberMe", false);
HttpPost httpost = new HttpPost(LOGIN_PATH);
StringEntity se = new StringEntity(data.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-Type", "application/json");
httpost.setHeader("Content-Length",""+se.getContentLength());
ResponseHandler responseHandler = new BasicResponseHandler();
return httpclient.execute(httpost, responseHandler);
当我设置 Content-Length 标头时,它给出“Content-Length 标头已经存在”,当删除 content-length 标头时,它给出“需要长度”
谢谢。