【问题标题】:HttpURLConnection : Server return HTTP 403 ForbiddenHttpURLConnection :服务器返回 HTTP 403 Forbidden
【发布时间】:2014-11-17 09:36:14
【问题描述】:

我想用他的 url 下载一个文件。 我使用带有 HttpURLConnection 的 AsyncTask,但是当我得到响应代码时,服务器返回错误 403。 我在 doInBackground 中使用 HttpURLConnection。

代码:

@Override
protected String doInBackground(String... sUrl) {
    InputStream input = null;
    OutputStream output = null;
    HttpURLConnection connection = null;
    try {

        ext = FilenameUtils.getExtension(sUrl[0]);
        fileName = FilenameUtils.getBaseName(sUrl[0]);

        Log.i("Brieg", "storage : /storage/emulated/0/" + fileName + "." + ext);

        URL url = new URL(sUrl[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
        }

        int fileLength = connection.getContentLength();

        input = connection.getInputStream();
        output = new FileOutputStream("/storage/emulated/0/" + fileName + "." + ext);

        byte data[] = new byte[4096];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {

            if (isCancelled()) {
                input.close();
                return null;
            }
            total += count;

            if (fileLength > 0)
                publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }
    }
    catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        try {
            if (output != null)
                output.close();
            if (input != null)
                input.close();
        }
        catch (IOException ignored) {
        }

        if (connection != null)
            connection.disconnect();
    }
    return null;

}

问题出在哪里?

知道当我在浏览器中获取 URL 时,下载文件就会启动。

提前谢谢你。

【问题讨论】:

  • sUrl[0] 请显示您的网址的样子。
  • output = new FileOutputStream("/storage/emulated/0/" + fileName + "." + ext);。使用这样的硬编码路径并不好。它会在许多设备上失败。它是什么样的服务器?它驻留在哪里(局域网/互联网)?
  • 我知道,我只是用它来测试但应用程序之前停止了。网址是这样的:mywebsite.com/myfile.epub

标签: android android-asynctask httpurlconnection http-status-code-403


【解决方案1】:

原因应该是你没有设置 User-Agent:

connection = (HttpURLConnection) url.openConnection();   
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:221.0) Gecko/20100101 Firefox/31.0"); // add this line to your code
connection.connect();

【讨论】:

    【解决方案2】:

    HTTP 403 禁止 问题出在哪里

    错误代码清楚地说明了 - 您被禁止访问服务器上的资源。也许您需要先进行身份验证,也许您只是被禁止了。多种可能性。

    【讨论】:

    • 当我在浏览器中获取 url 时,下载文件启动。
    • 您可能想调查一下您的浏览器到底在发送什么 - 可以是 cookie、referer、两者或任何其他服务器希望看到的东西,但由于您没有发送它,您会被拒绝。
    • @MarcinOrlowski 这个错误并不像你说的那么清楚。我有一个可以在浏览器中下载的文件,但不能通过代码下载。我有权限,甚至尝试设置用于访问服务器隐藏部分的身份验证和禁用 SSL 验证。与 OP 非常相似,这可能是 Android 下载尝试独有的问题,并且该错误是用词不当,因为它被发现但无法访问。
    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多