/// <summary> /// SSL 请求 /// </summary> /// <param name="client"></param> /// <param name="sendBody"></param> /// <param name="sendHeader"></param> /// <returns></returns> LxwResponse SendSSLRequest(TcpClient client, byte[] sendBody, LxwRequestHeader sendHeader, bool write = false) { SslStream sslStream = new SslStream(client.GetStream(), true, new RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) => sslPolicyErrors == SslPolicyErrors.None), null); sslStream.ReadTimeout = ReadTimeOut * 1000; sslStream.WriteTimeout = WriteTimeOut * 1000; X509Store store = new X509Store(StoreName.My); sslStream.AuthenticateAsClient( sendHeader.Uri.Host, store.Certificates, System.Security.Authentication.SslProtocols.Default, false); if (!sslStream.IsAuthenticated) { throw new Exception("sslStream.IsAuthenticated is false"); } if (write) { var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_ssl.data"); FileStream fs = new FileStream(file, FileMode.CreateNew); fs.Write(sendHeader.HeaderByte, 0, sendHeader.HeaderByte.Length); fs.Write(LINE, 0, LINE.Length); fs.Write(LINE, 0, LINE.Length); if (sendBody != null) { fs.Write(sendBody, 0, sendBody.Length); } fs.Write(LINE, 0, LINE.Length); fs.Close(); } { sslStream.Write(sendHeader.HeaderByte); sslStream.Write(LINE); sslStream.Write(LINE); if (sendBody != null) { sslStream.Write(sendBody); } sslStream.Write(LINE); } sslStream.Flush(); return(ReadResponse(sslStream)); }
/// <summary> /// 普通请求 /// </summary> /// <param name="client"></param> /// <param name="sendBody"></param> /// <param name="sendHeader"></param> /// <returns></returns> LxwResponse SendNoSSLRequest(TcpClient client, byte[] sendBody, LxwRequestHeader sendHeader, bool write = false) { NetworkStream stream = client.GetStream(); stream.ReadTimeout = WriteTimeOut * 1000; stream.WriteTimeout = WriteTimeOut * 1000; if (write) { var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_nossl.data"); FileStream fs = new FileStream(file, FileMode.CreateNew); fs.Write(sendHeader.HeaderByte, 0, sendHeader.HeaderByte.Length); fs.Write(LINE, 0, LINE.Length); fs.Write(LINE, 0, LINE.Length); if (sendBody != null) { fs.Write(sendBody, 0, sendBody.Length); } fs.Write(LINE, 0, LINE.Length); fs.Close(); } stream.Write(sendHeader.HeaderByte, 0, sendHeader.HeaderByte.Length); stream.Write(LINE, 0, LINE.Length); stream.Write(LINE, 0, LINE.Length); if (sendBody != null) { stream.Write(sendBody, 0, sendBody.Length); } stream.Write(LINE, 0, LINE.Length); stream.Flush(); return(ReadResponse(stream)); }
LxwRequestHeader FormatHeader(string request, byte[] body = null, Dictionary <string, string> KEYS = null, string boundary = null) { if (request == null) { throw new ArgumentNullException("byte[] FormatHeader(string request), request is null"); } request = FormatKeys(request, KEYS); LxwRequestHeader req = new LxwRequestHeader(Encoding); var bodys = Regex.Split(request, "\r\n"); var list = new List <string>(); var start = 0; var end = bodys.Length - 1; { for (; start < bodys.Length; start++) { if (bodys[start] != "") { break; } } for (; end > start; end--) { if (bodys[end] != "") { break; } } for (; start <= end; start++) { if (bodys[start].StartsWith("Content-Length", StringComparison.OrdinalIgnoreCase)) { continue; } if (bodys[start].StartsWith("Cookie:", StringComparison.OrdinalIgnoreCase)) { continue; } //Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryuM55vqcAjmXSlVHa if (bodys[start].ToLower().Contains("multipart/form-data")) { if (!string.IsNullOrEmpty(boundary)) { bodys[start] = "Content-Type: multipart/form-data; boundary=" + boundary; } list.Add(bodys[start]); continue; } { //这里还可以扩充 var arr = bodys[start].Split(' '); if (arr[0] == "GET" || arr[0] == "POST" || arr[0] == "OPTIONS") { req.SSL = arr[1].StartsWith("https://", StringComparison.OrdinalIgnoreCase); req.Uri = new Uri(arr[1]); //if (string.IsNullOrEmpty(boundary)) arr[1] = req.Uri.PathAndQuery; bodys[start] = string.Join(" ", arr); } } list.Add(bodys[start]); } if (body != null) { list.Add("Content-Length: " + body.Length); } //if (string.IsNullOrEmpty(boundary)) if (LST_COOKIE.Count > 0) { list.Add("Cookie: " + CreateCookies()); } //输出信息 Debug.WriteLine(string.Join("\r\n", list.ToArray())); req.Header = string.Join("\r\n", list.ToArray()); //req.HeaderByte } //返回 return(req); }