android - http删除tomcat 403错误

标签 android http http-delete

我试图从我的 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();

                }

最佳答案

在 web.xml 中,启用其他 http 方法:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <init-param>
        <param-name>debug</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>readonly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

参数debuglistings是tomcat默认加载的,而默认的readonly为true,表示只有GET和POST可用.

其他可用的参数是:

     debug               Debugging detail level for messages logged     
                         by this servlet.  [0]                          

     fileEncoding        Encoding to be used to read static resources   
                         [platform default]                             

     input               Input buffer size (in bytes) when reading      
                         resources to be served.  [2048]                

     listings            Should directory listings be produced if there 
                         is no welcome file in this directory?  [false] 
                         WARNING: Listings for directories with many    
                         entries can be slow and may consume            
                         significant proportions of server resources.   

     output              Output buffer size (in bytes) when writing     
                         resources to be served.  [2048]                

     readonly            Is this context "read only", so HTTP           
                         commands like PUT and DELETE are               
                         rejected?  [true]                              

     readmeFile          File to display together with the directory    
                         contents. [null]                               

     sendfileSize        If the connector used supports sendfile, this  
                         represents the minimal file size in KB for     
                         which sendfile will be used. Use a negative    
                         value to always disable sendfile.  [48]        

     useAcceptRanges     Should the Accept-Ranges header be included    
                         in responses where appropriate? [true]         

关于android - http删除tomcat 403错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308436/

相关文章:

android - 如何从 Android 主题中提取颜色值 (#rgb)?

android - fragment 实例中带有 Otto 事件总线的 IllegalArgumentException

angularjs - 如何使用 Angular.js 发出 HTTP 请求?

spring - 如何在 Spring MVC REST Controller 中访问 HTTP header 信息?

c# - Web API 2 DELETE 方法总是返回 500

java - Kotlin - 我们如何使用 getter 和 setter 访问私有(private)属性(property)?访问方法是否在内部调用?

http - 使用 Nginx Lua 模块写入文件

spring - Spring Data 中的删除操作如何与 Rest 配合使用

angular - 传递一个包含 json 项目数组的主体,以 Angular 形式删除

android - 如何确定当前是否正在使用 Android 应用程序?