我的cURL:
curl URL -X POST -d'{“经度”:“5.55”,“纬度”:“6.66”,“地方”:“你好世界”,“描述”:“这个地方可以通过轮椅进入”} '-u user:password -H'Content-type:application / json'
我的Java代码:
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("user", "password"));
HttpPost httppost = new HttpPost("URL");
httppost.setHeader("Content-type", "application/json");
JSONObject json = new JSONObject();
try {
json.put("longitude", "1");
json.put("latitude", "2");
json.put("place", "3");
json.put("description", "4");
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
try {
StringEntity entity = new StringEntity(json.toString(), HTTP.UTF_8);
// entity.setContentType("application/json");
httppost.setEntity(entity);
HttpResponse resp = httpclient.execute(httppost);
System.out.println(resp.getStatusLine().getStatusCode() + "/"
+ resp.getStatusLine().getReasonPhrase());
} catch (Exception e) {
System.err.println(e.getMessage());
}
System.out.println("SUCCESS: " + result);
我的cURL已正确执行但JAVA代码仍然返回403错误。 请帮助=。=
答案 0 :(得分:0)