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();
}