从我的 android 应用程序中,我试图删除存储在 tomcat 的 webapps 目录中的图像。当我尝试以下代码时,它给了我 403 状态代码。我在网上查了一下,发现如果请求合法但该操作被禁止,它会给出该代码。谁能告诉我哪里出错了。我的代码是:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
int responseCode = connection.getResponseCode();
当我尝试使用 HttpClient 时,它给了我同样的错误-HTTP/1.1 403 Forbidden
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.getParams().setParameter(
"http.socket.timeout", new Integer(90000));
HttpDelete delete = new HttpDelete(new URI(
"http://192.168.2.1:9090/LocationUpdaterServlet/images/"
+ userid));
Toast.makeText(Image.this, "Removing your picture", 5000).show();
HttpResponse response = httpClient.execute(delete);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.out.println(response.getStatusLine());
} else {
// Here every thing is fine.
}
HttpEntity resEntity = response.getEntity();
if (resEntity == null)
System.out
.println("---------Error No Response !!!-----");
}catch (Exception ex) {
System.out.println("---------Error----"+ ex.getMessage());
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}