【问题标题】:Download a file through http-auth通过 http-auth 下载文件
【发布时间】:2015-08-27 10:13:26
【问题描述】:

我无法使用基本的 http-auth 下载文件。我有用于授权的 auth 字符串,但出现异常:FileNotFound。 代码:

URL url = new URL(params[0]);
                URLConnection  conection =  url.openConnection();

                conection.setRequestProperty("Authorization", authString);
                conection.setRequestMethod("GET");
                conection.setAllowUserInteraction(false);
                conection.setDoInput(true);
                conection.setDoOutput(true);
                conection.connect();



                int lenghtOfFile = conection.getContentLength();


                // Exception on line below
                BufferedInputStream  input = new BufferedInputStream(conection.getInputStream()); 

                // Output stream
                OutputStream output = new FileOutputStream(filePath);

                byte data[] = new byte[1024];
...

lenghtOfFile 不为 0。此外,我尝试使用 HtppClient 执行此任务,但也出现异常。

【问题讨论】:

  • 删除 setDoOutput(true) 行。
  • 谢谢,对我有帮助)

标签: java android http download http-authentication


【解决方案1】:

正如我所评论的,您应该删除 setDoOutput(true) 行,因为这是 GET 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多