Boy I sure hope you can help. I love the following for simple file download:
$client = new-object System.Net.WebClient $client.DownloadFile($url, $path)
But I have the server set up to use SSL client certificate mapping, and it works!!
EXCEPT for actually saving the text files it is trying to download.
I had to make the SSL connection the hard way:
$cert = dir cert:\CurrentUser\My | where {$_.Subject -like "*-auth-cert-name-*"}
$computerName = "--URL--Here--"
$port = "443"
[System.Security.Authentication.SslProtocols]$protocol = "ssl3"
$certcol = New-object System.Security.Cryptography.X509Certificates.X509CertificateCollection
$certcol.Add($cert)
$socket = New-Object Net.Sockets.TcpClient($computerName, $port)
$stream = $socket.GetStream()
$sslStream = New-Object System.Net.Security.SslStream $stream,$false
$sslStream.AuthenticateAsClient($computerName,$certcol,$protocol,$false)
$sslStream
This part is working. I wish I could just overload System.Net.WebClient with my SslStream object somehow and call the good old DownloadFile() method, but I cannot apparently. So I am stuck, like in "HTTP 101" class, trying to manage the stream in nested loops.
I need to manually read HTML headers and try to exclude them and then save the rest of the Content-Length to a file without buffer overruns.
Do you know how to do this? If the files are small, I can make it work, but as soon as I get close to unsigned INT lengths all the .NET stream reader functions I call crap out and overrun trashing memory and the rest of the script.
Please help
function GetTextOutput($named) {
$buffer = new-object System.Byte[] 4096
$encoding = new-object System.Text.AsciiEncoding
$outputBuffer = ""
$foundMore = $false
$outarray = @()
$contentsize = 0
do {
start-sleep -m 1000
$foundmore = $false
$sslstream.ReadTimeout = 1000
do {
try {
$read = $sslstream.Read($buffer, 0, 4096)
if($read -gt 0) {
$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
} catch { $foundMore = $false; $read = 0 }
} while($read -gt 0)
} while($foundmore)
# Read response into array, line by line
$txtreader = new-object system.IO.StringReader($outputBuffer)
while ( ($txtreader.Peek() -ne -1) ){
$outarray += $txtreader.ReadLine()
}
return $outarray
}