Is it possible to make a downloadstring() and using gzip-compression, if the server is accepting this?
$wc = New-Object System.Net.WebClient
$wc.Encoding = [System.Text.Encoding]::UTF8
$wc.Headers.Add("User-Agent: Other")
$qc = $wc.Downloadstring($url)
Does anyone know the correct Headers.Add or what do we have to add?
Try this:
$url = "http://www.somewebsite.com/"
$wc = New-Object System.Net.WebClient
$wc.Headers.Add([System.Net.HttpRequestHeader]::AcceptEncoding, "gzip")
$wc.Headers.Add("User-Agent: Other")
$qc = $wc.Downloadstring($url)
Alternatively you can try and use DownloadFile method to see if this yields an expected result:
$wc.DownloadFile($url, "c:\temp\dump.txt" )
If there are errors then update your question to include them.
Related
I am trying to download jenkins.war file through powershell on windows server, but I get 401 Unauthorized error in downloadFile. I passed correct network credentials still getting this error. Below is my code:
$source = "http://mirrors.jenkins.io/war-stable/latest/jenkins.war"
$destination = "$jenkinsDir\jenkins.war"
$client = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential("admin", "P#ssw0rd")
$credCache.Add("http://mirrors.jenkins.io/war-stable/latest/jenkins.war", "Basic", $creds)
$client.Credentials = $credCache
$client.downloadFile($source, $destination)
How to get rid of this 401 error?
I also tried this link System.Net.WebClient doesn't work with Windows Authentication but no luck.
For a reporting tool I need to download an XML which is only accessible through a SOCKS proxy. My idea was to install the proxy in IE but disable it, then in powershell enable it a few seconds so I download it, but that doesn't seem to work. Anybody got any other work-arounds?
Here is my code
$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true
$url = "https://appmon-apm.####.##/rest/management/dashboard/xmlgen"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$str
Pretty new to PowerShell and hoping someone can point me in the right direction.
I need to perform a POST request and have to pass it a locally stored cert (x509) during the POST request, for authentication.
What is the best way or way to accomplish this? I've found plenty of example to be able to perform this task in .net/C# but I am not finding anything that will accomplish this task in PowerShell.
Here is my POST request code, again I would like to point to a cert stored locally "C:\code\cert.crt" and pass it during the web transaction.
$url = "https://myUrl/uploadTester"
$data = '{"data": "988309487577839444"}'
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$b = [System.Text.Encoding]::ASCII.GetBytes($data)
$web = [System.Net.WebRequest]::Create($url)
$web.Method = "POST"
$web.ContentLength = $b.Length
$web.ContentType = "application/x-www-form-urlencoded"
$stream = $web.GetRequestStream()
$stream.Write($b,0,$b.Length)
$stream.close()
$reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
$reader.ReadToEnd()
$reader.Close()
Thanks for all the help in advanced.
It's pretty easy to convert C# to PowerShell. Give this a try:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile("C:\Users\Andy\Desktop\Test.cer")
$web = [System.Net.WebRequest]::Create($url)
$web.ClientCertificates.Add($Cert)
I adapted this from: http://support.microsoft.com/kb/895971
Looks like the key is the ClientCertificates property.
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.clientcertificates.aspx
I'm having a hard time modifying PsGet to work though a proxy. I replaced every $client initialization on PsGet.psm1 with this
# $client = (new-object Net.WebClient)
$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true
$client = new-object system.net.WebClient
$client.proxy = $proxy
but I still keep getting the DotNetMethodException during the WebClient request.
Give this a try. Use [System.Net.WebRequest]::DefaultWebProxy instead of the registry read.
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("User-Agent","Mozilla/4.0+")
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.DownloadString("https://github.com/chaliy/psget/raw/master/PsGet/PsGet.psm1")
In C#, I might do something like this:
System.Net.WebClient w = new System.Net.WebClient();
w.Credentials = new System.Net.NetworkCredential(username, auth, domain);
string webpage = w.DownloadString(url);
Is there a Powershell version of this, or should I just call through to the CLR?
The PowerShell is almost exactly the same.
$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$webpage = $webclient.DownloadString($url)
For those that need Powershell to return additional information like the Http StatusCode, here's an example. Included are the two most likely ways to pass in credentials.
Its a slightly modified version of this SO answer:
How to obtain numeric HTTP status codes in PowerShell
$req = [system.Net.WebRequest]::Create($url)
# method 1 $req.UseDefaultCredentials = $true
# method 2 $req.Credentials = New-Object System.Net.NetworkCredential($username, $pwd, $domain);
try
{
$res = $req.GetResponse()
}
catch [System.Net.WebException]
{
$res = $_.Exception.Response
}
$int = [int]$res.StatusCode
$status = $res.StatusCode
return "$int $status"
In some case NTLM authentication still won't work if given the correct credential.
There's a mechanism which will void NTLM auth within WebClient, see here for more information: System.Net.WebClient doesn't work with Windows Authentication
If you're trying above answer and it's still not working, follow the above link to add registry to make the domain whitelisted.
Post this here to save other's time ;)