我发现了很多请求REST API的示例,但是它们在一起令人困惑。有没有人能解释一下如何使用HTTP请求呢?
我的需求是,通过提供用户名、密码和密钥从REST API获取数据。
我所使用的是:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
但是响应总是为空,当使用浏览器工具发布相同的数据时,这些工作正常。我的方法有问题吗?请建议我正确的方法。谢谢。