Не удается скачать файл с URL - PullRequest
       12

Не удается скачать файл с URL

0 голосов
/ 06 сентября 2018

Мне нужно скачать файл с URL. Я написал следующий код. Обратите внимание, что целевой путь C: \ test уже существует

$url = "https://www.bergeyselectric.com/content/wp-content/uploads/2011/11/google.jpg"
$downloadpath = "C:\test"
$filename = $url.Substring($url.LastIndexOf("/") + 1)

$downladfileWithPath   = Join-Path $downloadPath $filename

Try
{
   (New-Object System.Net.WebClient).DownloadFile($url, $downladfileWithPath)
}
Catch
{
    Write-Host $_.Exception | format-list -force
}

Я получаю следующее исключение.

System.Management.Automation.MethodInvocationException: Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error 
occurred on a send." ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to rea
d data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was f
orcibly closed by the remote host
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at CallSite.Target(Closure , CallSite , Object , Object , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

Что я здесь не так делаю?

1 Ответ

0 голосов
/ 06 сентября 2018

Вероятно, это проблема TLS. Это работает для меня

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri "https://www.bergeyselectric.com/content/wp-content/uploads/2011/11/google.jpg" -OutFile c:\temp\x.jpg
...