403从Android应用访问Web服务时出错

时间:2015-01-20 03:09:03

标签: android

我在Spring中构建一个简单的Web服务并从Android访问它。

我的网络服务可以在我的浏览器和Android模拟器浏览器中正常运行。但是从我的Android应用程序中,我收到403错误。

我的应用程序有Internet Permision。

这是我的代码:

protected String doInBackground(String... urls){

    String u = urls[0];
    HttpClient httpclient = new DefaultHttpClient();
    StringBuilder builder = new StringBuilder();
    HttpPost httppost = new HttpPost(u);
    System.out.println("URL: " + u);

    try { 
        HttpResponse respuesta = httpclient.execute(httppost);
        StatusLine statusLine = respuesta.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        System.out.println("Status : " + statusCode);

        if (statusCode == 200){
            HttpEntity entity = respuesta.getEntity();
            InputStream contenido = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(contenido));

            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                System.out.println("Linea recibida : " + line);
            }
        }   
    } catch (Exception e){
         System.out.println("Primer catch");
         e.printStackTrace();
    }

    return builder.toString();
}

1 个答案:

答案 0 :(得分:0)

403响应表示您与之连接的主机或软件不允许访问该网络服务。原因可能是:

  • 您的远程主机被防火墙阻止
  • 您已达到对网络服务的限制,并且阻止了任何进一步的连接
  • 您正在发送身份验证凭据,但该帐户已被禁止/禁用
  • 请求中的用户代理或其他标头导致服务拒绝连接。也许缺少标题?