【发布时间】:2011-05-11 01:17:34
【问题描述】:
我需要创建一个带有参数的 HTTP POST 请求。我知道那里有很多示例,我尝试过使用 HTTPparams、NameValuePair 等,但似乎无法为服务器获取正确的格式。
Server Type: REST based API utilizing JSON for data transfer
Content-type: application/json
Accept: application/json
Content-length: 47
{"username":"abcd","password":"1234"}
我可以传递这些标题,但我似乎无法传递这些参数“用户名”、“密码”。这是我的代码:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mymi5.net/API/auth/login");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username","abcd"));
pairs.add(new BasicNameValuePair("password","1234"));
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,"UTF-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
我尝试调试,但无法查看实体是否正确附加...我做错了什么?
提前致谢。 马兹
【问题讨论】: