in Eski Blog Yazılarım

İpucu: HttpWebRequest ile https

HttpWebRequest sınıfı ile https üzerinden veri alışverişinde bulunduysanız muhakkak karşılaşmışsınızdır.

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

Bu durumu aşağıdaki event’a anonim bir delegate tanımlayarak bypass edebiliyoruz.

            System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate
(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{ return true; };


var parameters = "go=1";

var cred = new NetworkCredential();
cred.Password = "demo"
cred.UserName = "admin"

var request = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com");
request.AllowAutoRedirect = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = cred;
request.Method = "POST";
request.UserAgent = "Vargonen/0.1";
request.ContentLength = parameters.Length;

var reqStream = request.GetRequestStream();
reqStream.Write(parameters, 0, parameters.Length);
reqStream.Close();

var response = (HttpWebResponse)request.GetResponse();
var sr = new StreamReader(response.GetResponseStream());
var htmlText = sr.ReadToEnd().Trim();

http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback.aspx

Yorum Bırak

Comment