【问题标题】:How to get the info if the ftp path is not accessible?如果 ftp 路径不可访问,如何获取信息?
【发布时间】:2019-03-27 08:19:28
【问题描述】:

我正在尝试从 ftp 路径下载 Acrobat Reader。有时 ftp 路径没有响应。如果没有响应,我想获取信息。

$ftpFolderUrl = "ftp://ftp.adobe.com/pub/adobe/reader/win/AcrobatDC/"
$ftpRequest = [System.Net.FtpWebRequest]::Create("$ftpFolderUrl")
$ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftpResponse = $ftpRequest.GetResponse()
$responseStream = $ftpResponse.GetResponseStream()

如何获取 ftp 是否响应的信息?

请帮忙。

【问题讨论】:

    标签: powershell ftp ftpwebrequest


    【解决方案1】:

    您可以使用 try 和 catch。
    首先尝试获得响应,如果这不起作用,请打印异常。
    类似的东西:

    $ftpFolderUrl = "ftp://ftp.adobe.com/pub/adobe/reader/win/AcrobatDC/"
    $ftpRequest = [System.Net.FtpWebRequest]::Create("$ftpFolderUrl")
    $ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
    try
    {
        $ftpResponse = $ftpRequest.GetResponse()
        $responseStream = $ftpResponse.GetResponseStream()
    }
    catch
    {
        $_.Exception | format-list -force
    }
    

    这将打印出任何错误。
    如果您想变得花哨,现在可以处理抛出的错误并单独捕获它们。

    因此,请查看此页面:try and catchexceptions

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多