Witajcie, Wesołych Świąt!
Oto przykładowy kod , który powinien pobierać mi plik po https, np ten: https://github.com/ALIENQuake/WindowsPowerShell/blob/master/Microsoft.PowerShell_profile.ps1
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.ComponentModel;
using System.Windows;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;
namespace wp3
{
class Program
{
static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
static void Main(String[] args)
{
try
{
string urlHttps = args[0];
//Create an request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlHttps);
//Set default credential is true
request.UseDefaultCredentials = true;
request.Credentials = CredentialCache.DefaultCredentials;
//Set validation callback
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream output = File.Open("tmp.dat", FileMode.OpenOrCreate))
using (Stream input = response.GetResponseStream())
{
byte[] buffer = new byte[512];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
catch (Exception)
{
}
}
}
}
Niestety nie działa ale działa kiedy podam inny link bez https(plik tmp.dat zawiera dane).
Sprawdziałem też pobieranie pliku przez IE oraz takie polecenie WGET:
Wget --no-check-certificate https://raw.githubusercontent.com/ALIENQuake/WindowsPowerShell/master/Microsoft.PowerShell_profile.ps1
i w obu przypadkach plik się pobiera. Czy ktoś dysponuje działającym przykład, ew pomoże w poprawieniu kodu ?
dodanie znaczników <code class="csharp"> i <code> - furious programming