Powershell-¿Por Qué Usar Invoke-WebRequest Es Mucho Más Lento Que una Descarga del Navegador?


Utilizo el método Invoke-WebRequest de Powershell para descargar un archivo de Amazon S3 a mi instancia de Windows EC2.

Si descargo el archivo usando Chrome, puedo descargar un archivo de 200 MB en 5 segundos. La misma descarga en PowerShell usando Invoke-WebRequest tarda hasta 5 minutos.

¿Por qué usar Invoke-WebRequest es más lento y hay una forma de descargar a toda velocidad en un script de PowerShell?

Author: Lloyd Banks, 2015-02-23

4 answers

Sin cambiar de Invoke-WebRequest, apagar la barra de progreso lo hizo por mí. Encontré la respuesta de este hilo: https://github.com/PowerShell/PowerShell/issues/2138 (jasongin comentó el 3 de oct de 2016)

$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest <params>

Para mi archivo de 5MB en localhost, el tiempo de descarga pasó de 30s a 250ms.

Tenga en cuenta que para recuperar la barra de progreso en el shell activo, debe llamar a $ProgressPreference = 'Continue'.

 23
Author: TinyTheBrontosaurus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-18 16:13:06

Estaba usando

Invoke-WebRequest $video_url -OutFile $local_video_url

Cambié lo anterior a

$wc = New-Object net.webclient
$wc.Downloadfile($video_url, $local_video_url)

Esto restableció la velocidad de descarga a lo que estaba viendo en mis navegadores.

 22
Author: Lloyd Banks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-03 04:57:12

Acabo de golpear este problema hoy, si cambia el argumento ContentType a application/octet-stream es mucho más rápido (tan rápido como usar webclient). La razón se debe a que el comando Invoke-Request no intentará analizar la respuesta como JSON o XML.

Invoke-RestMethod -ContentType "application/octet-stream" -Uri $video_url  -OutFile $local_video_url
 1
Author: user1875674,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-02 10:32:40

Asumiría que el razonamiento detrás de por qué Invoke-WebRequest es más lento es su análisis de estilo scraper de la página que está solicitando, mientras que el cliente web.NET solo está emitiendo una solicitud get y guardando la respuesta.

Invoke-WebRequest analiza las propiedades Content, ParsedHtml, Forms, InputFields, Links, Images, Scripts, AllElements, BaseResponse y statusCode.

Bastante bueno artículo sobre raspado con Invoke-WebRequest.

 0
Author: cchamberlain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-08 23:45:51