Return to Snippet

Revision: 68778
at February 23, 2015 04:50 by jatubio


Updated Code
# Setup one timeout value for downloadstring method of WebClient Class
#
# Property Timeout of WebClient isn't public, but we can inhertis from him and change this property in the new class
#
# Inspired by: One article on What Was I Thinking? (http://goo.gl/IQQazD)
# Thanks also to Stefan Go�ner (http://goo.gl/T6RZJC)
#
# run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 100 "http://www.google.es"
# run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 -url "http://www.google.es" -timeout -1
Param(
  [string]$timeout, 	#input parameter (Download String Timeout).
  [string]$url			#input parameter (url to Download).
)
     

$Source = @"
	using System.Net;

	public class ExtendedWebClient : WebClient
	{
		public int Timeout;

		protected override WebRequest GetWebRequest(System.Uri address)
		{
			WebRequest request = base.GetWebRequest(address);
			if (request != null)
			{
				request.Timeout = Timeout;
			}
			return request;
		}

		public ExtendedWebClient()
		{
			Timeout = 100000; // the standard HTTP Request Timeout default
		}
	}
"@;

Add-Type -TypeDefinition $Source -Language CSharp  

$webclient = New-Object ExtendedWebClient;
$webclient.Timeout=$timeout;
$webclient.downloadstring($url);
$webclient

Revision: 68777
at February 23, 2015 03:16 by jatubio


Initial Code
# Setup one timeout value for downloadstring method of WebClient Class
#
# Property Timeout of WebClient isn't public, but we can inhertis from him and change this property in the new class
#
# Inspired by: One article on What Was I Thinking? (http://goo.gl/IQQazD)
# Thanks also to Stefan Goßner (http://goo.gl/T6RZJC)
#
# run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 100 "http://test.jatubio.com/yasni/google1.php?instancia=pw"
# run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 -url "http://test.jatubio.com/yasni/google1.php?instancia=pw" -timeout -1
Param(
  [string]$timeout, 	#input parameter (Download String Timeout).
  [string]$url			#input parameter (url to Download).
)
     

$Source = @"
	using System.Net;

	public class ExtendedWebClient : WebClient
	{
		public int Timeout;

		protected override WebRequest GetWebRequest(System.Uri address)
		{
			WebRequest request = base.GetWebRequest(address);
			if (request != null)
			{
				request.Timeout = Timeout;
			}
			return request;
		}

		public ExtendedWebClient()
		{
			Timeout = 100000; // the standard HTTP Request Timeout default
		}
	}
"@;

Add-Type -TypeDefinition $Source -Language CSharp  

$webclient = New-Object ExtendedWebClient;
$webclient.Timeout=$timeout;
$webclient.downloadstring($url);
$webclient

Initial URL

                                

Initial Description
Setup one timeout value for downloadstring method of WebClient Class in PowerShell   

Property Timeout of WebClient isn't public, but we can inhertis from him and change this property in the new class  

Inspired by: One article on What Was I Thinking? (http://goo.gl/IQQazD)  
Thanks also to Stefan Goßner (http://goo.gl/T6RZJC)  

run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 100 "http://www.google.es"  
run sample: powershell.exe -executionpolicy bypass .\DownloadString.ps1 -url "www.google.es" -timeout -1

Initial Title
Setup one timeout value for downloadstring method of WebClient Class  in PowerShell

Initial Tags

                                

Initial Language
Windows PowerShell