“调用“下载文件"的异常与“2"参数:“在 WebClient 请求期间发生异常." [英] "Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."

查看:118
本文介绍了“调用“下载文件"的异常与“2"参数:“在 WebClient 请求期间发生异常."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置下载需要的文件以自动化和安装过程.每当我运行脚本时,我总是遇到错误,从星期天开始我已经改变了七种方式,但它仍然给我错误.

I am trying to setup to download need files to automate and install process. I keep getting errors ever time I run the script and I have changed it seven ways from sunday and it still gives me errors.

脚本是:

  if (test-path $java_path)

   {
   Write-Output "Java already installed. Skipping script"
   exit 0
    }

  else 

    {
      $source = "http://our.server.com/java-installer.zip"
      $destination = "c:\CHPACS"
      $client = new-object System.Net.WebClient
      $client.DownloadFile($source, $destination)

      }

我收到的错误信息是

 Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
 At C:\ps_script\testjava.ps1:41 char:31
 +           $client.DownloadFile <<<< ($source, $destination)
 + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : DotNetMethodException

我是否需要创建一个函数才能使其正常工作?

Do I need to create a function to make this work properly?

谢谢:

推荐答案

如果您查看 DownloadFile 方法的 MSDN 文档,您会注意到第二个参数是一个 文件名,不是目录.因此,如果您将 $destination 重新定义为:

If you look at the MSDN documentation for the DownloadFile method, you'll notice that the second parameter is a filename, not a directory. So, if you re-define $destination to something like:

$destination = "c:\CHPACS\java-installer.zip"

那么它应该可以工作.

其次检查您尝试下载的文件是否当前未打开或正在执行.如果文件正在使用中,则会引发此异常.

Check secondly that the file that you're trying to download isn't open or being executed at the moment. This exception will be raised if the file is in use.

这篇关于“调用“下载文件"的异常与“2"参数:“在 WebClient 请求期间发生异常."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆