问题描述
下面是我的code为POST请求到服务器。
Here is my code for the POST request to the server.
该JSON被张贴到服务器:
The JSON to be posted to the server :
{
"User": {
"Name": "dog","Password": "123" }
}
我如何正在创建JSON对象
How I am creating the JSON Object
object = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Name", "dog");
jsonObject.put("Password", "123");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
object.put("User", jsonObject);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
发布到服务器
public HttpResponse request(JSONObject request)
throws ClientProtocolException, IOException, IllegalStateException,
JSONException {
client = (DefaultHttpClient) WebClientDevWrapper
.getNewHttpClient();
HttpPost post = new HttpPost(
"https://wbapi.cloudapp.net:443/api/User/LocalLogin/");
post.setEntity(new StringEntity(request.toString(), "utf-8"));
HttpResponse response = client.execute(post);
return response;
}
随着由帕雷什Mayani这里提供 HTTPS POST请求到服务器
我收到响应对象。但我的response.getStatusLine()不断出现500 /内部服务器错误只POST请求。
I am getting the response object. But my response.getStatusLine() keeps showing 500/ Internal server error for only POST requests.
请注意:GET请求工作正常
Note : GET requests are working fine.
什么是我的code中的问题?我怎样才能解决这个问题?
What is the Problem in my code ? How can I tackle this error ?
变更请求code作为(所推荐的Bhavdip Pathar)
Changed the request code as ( As recommended by Bhavdip Pathar)
public HttpResponse request(JSONObject request)
throws ClientProtocolException, IOException, IllegalStateException,
JSONException {
HttpPost post = new HttpPost(
"https://wbapi.cloudapp.net:443/api/User/LocalLogin/");
// post.setEntity(new StringEntity(request.toString(), "utf-8"));
StringEntity entity = new StringEntity(request.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
post.setHeader("Content-Type", "application/json");
post.setHeader("Accept", "application/json");
post.setEntity(entity);
HttpResponse response = client.execute(post);
return response;
}
我没设置应用程序/ JSON,瞧,我得到HTTP / 200 OK。
I did set application/json and voila , I am getting HTTP/200 OK.
推荐答案
我觉得你应该设置的contentType:应用/ JSON可能是帮助,请尝试这种
I think you should set contentType: "application/json" might be help please try this.
感谢您
这篇关于与POST请求500内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!