/// <summary> /// Cria a stream para a uri no socket informado. /// </summary> /// <param name="uri"></param> /// <param name="socket"></param> /// <returns></returns> private async Task <System.IO.Stream> CreateStream(Uri uri, System.Net.Sockets.Socket socket) { System.IO.Stream stream = new System.Net.Sockets.NetworkStream(socket); if (uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { var sslStream = new System.Net.Security.SslStream(stream); await sslStream.AuthenticateAsClientAsync(uri.Host, new System.Security.Cryptography.X509Certificates.X509CertificateCollection(), _sslProtocols, checkCertificateRevocation : false); stream = sslStream; } return(stream); }
public static void AuthenticateAsClient(this System.Net.Security.SslStream stream , string targetHost) { Task.Run(() => stream.AuthenticateAsClientAsync(targetHost)).Wait(); }
public static IAsyncResult BeginAuthenticateAsClient(this System.Net.Security.SslStream stream , string targetHost , AsyncCallback callback, object state) { return(stream.AuthenticateAsClientAsync(targetHost).AsApm(callback, state)); }
private static String RequestGet(String url) { var str = ""; try { if (url.StartsWith("https://")) { #region HTTPS请求 var uri = new Uri(url); var hostSocket = Wlniao.Net.WlnSocket.GetSocket(uri.Host, uri.Port); var reqStr = ""; reqStr += "GET " + uri.PathAndQuery + " HTTP/1.1"; reqStr += "\r\nHost: " + uri.Host; reqStr += "\r\nDate: " + DateTools.ConvertToGMT(DateTools.GetUnix()); reqStr += "\r\nAccept: application/json"; reqStr += "\r\n"; reqStr += "\r\n"; var request = System.Text.Encoding.UTF8.GetBytes(reqStr); using (var ssl = new System.Net.Security.SslStream(new System.Net.Sockets.NetworkStream(hostSocket, true), false, new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate), null)) { ssl.AuthenticateAsClientAsync(uri.Host).ContinueWith((_rlt) => { if (ssl.IsAuthenticated) { ssl.Write(request); ssl.Flush(); var length = 0; var end = false; var start = false; var chunked = false; while (true) { var rev = new byte[65535]; var index = ssl.Read(rev, 0, rev.Length); if (index == 0) { break; } var beffur = new byte[index]; Buffer.BlockCopy(rev, 0, beffur, 0, index); var tempstr = strUtil.GetUTF8String(beffur); var lines = tempstr.Split(new string[] { "\r\n" }, StringSplitOptions.None); index = 0; #region Headers处理 if (!start && lines[0].StartsWith("HTTP")) { var ts = lines[0].Split(' '); if (ts[1] == "200") { for (index = 1; index < lines.Length; index++) { if (lines[index].ToLower().StartsWith("content-length")) { ts = lines[index].Split(' '); length = cvt.ToInt(ts[1]); } else if (lines[index].ToLower().StartsWith("transfer-encoding")) { chunked = lines[index].EndsWith("chunked"); } if (string.IsNullOrEmpty(lines[index])) { index++; start = true; break; } } } else { index = lines.Length; break; } } #endregion #region 取文本内容 for (; index < lines.Length; index++) { var line = lines[index]; if (chunked) { index++; if (index < lines.Length) { var tempLength = cvt.DeHex(line, "0123456789abcdef"); if (tempLength > 0) { length += (int)tempLength; line = lines[index]; } else if (lines.Length == index + 2 && string.IsNullOrEmpty(lines[index + 1])) { end = true; break; } else { break; } } else { break; } } if (index == 0 || (chunked && index == 1) || str.Length == 0) { str += line; } else { str += "\r\n" + line; } if (!chunked && System.Text.Encoding.UTF8.GetBytes(str).Length >= length) { end = true; } } if (end) { break; } #endregion } } }).Wait(); } hostSocket.Using = false; #endregion } else { #region HTTP请求 var response = new System.Net.Http.HttpClient().GetAsync(url).GetAwaiter().GetResult(); str = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); #endregion } if (string.IsNullOrEmpty(str)) { str = "{\"success\":false,\"message\":\"empty response\",\"data\":\"\"}"; } else { log.Info(str); } } catch (Exception ex) { str = "{\"success\":false,\"message\":\"request exception\",\"data\":\"" + ex.Message + "\"}"; } return(str); }
public async Task AuthenticateAsClientAsync(string targethost, System.Net.Security.RemoteCertificateValidationCallback validationCallback) { ssl = new System.Net.Security.SslStream(UnderlyingStream, false, validationCallback, null); await ssl.AuthenticateAsClientAsync(targethost, null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3, false); UnderlyingStream = ssl; }
public async Task AuthenticateAsClientAsync(string targethost) { ssl = new System.Net.Security.SslStream(UnderlyingStream, false, (s, c, h, l) => true, null); await ssl.AuthenticateAsClientAsync(targethost, null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Ssl3, false); UnderlyingStream = ssl; }