Accessing the Internet from PowerShell: WebClient options
This will be very quick, as there isn't a lot in terms of options for the WebClient. When accessing the internet, there is often the need to change certain parameters to meet out needs.
Proxy Server Settings
The WebClient class is very smart, by default it will make use of your Internet Explorer/System proxy settings, thus your scripts which use this object will automatically get what ever proxy you have configured there. This is handy, as you will not have to rewrite any scripts you might have if you ever need to have them run from behind a (corporate) proxy server.
There has only been a few instances where I have needed to every change the proxy settings that were in use, and in that case I wanted to switch off any proxy support. To do this, simply set the proxy to $null
Headers
By default, there are a number of headers that .Net will put into a web request for us, we can overwrite those by adding entries to the headers property.
Some Examples include:
The first one is setting the content type for the request, this is important when we are submitting data to a web page, as we will see later.
The second one is setting the user-agent header, this one is basically what browser will be reported to the server when we connect to it. By Default, there will be now user-agent specified in the header, and this can cause us issues as a number of sites expect valid browser user-agent strings. A number of security products will also expect this (Especially cloudflare). Another reason to set something different is to make troubleshooting easier, all servers will log the agent-string, and if you are looking at your web server logs to determine why a script isn't working, then this will can be of assistance.
Encoding
There is a number of different ways to encode the content of a web page, if you are submitting content to a web page, check if it is expecting a certain encoding format. Most these days expect UTF8, to ensure the WebClient uses this, specify the following:
Now you know how to change some of the settings for the WebClient, you can start to make use of the client. Next time we will look at downloading web pages and files.