public static byte[] Get(IPEndPoint endpoint, HttpArgs args) { return InternalSocketHttp(endpoint, args, HttpMethod.GET); }
public static byte[] Get(IPEndPoint endpoint, HttpArgs args, X509CertificateCollection certificates) { return InternalSslSocketHttp(endpoint, certificates, args, HttpMethod.GET); }
/// <summary> /// 解析 Ssl Response /// </summary> /// <param name="endpoint"></param> /// <param name="ssl"></param> /// <param name="args"></param> /// <param name="certificates"></param> /// <returns></returns> private static byte[] ParseSslResponse(IPEndPoint endpoint, SslStream ssl, HttpArgs args, X509CertificateCollection certificates) { //尝试10秒时间读取协议头 CancellationTokenSource source = new CancellationTokenSource(); Task<string> myTask = Task.Factory.StartNew<string>( new Func<object, string>(ReadSslHeaderProcess), ssl, source.Token); if (myTask.Wait(10000)) { string header = myTask.Result; if (header.StartsWith("HTTP/1.1 302")) { int start = header .ToUpper().IndexOf("LOCATION"); if (start > 0) { string temp = header.Substring(start, header.Length - start); string[] sArry = Regex.Split(temp, "\r\n"); args.Url = sArry[0].Remove(0, 10); return Get(endpoint, args, certificates); //注意:302协议需要重定向 } } else if (header.StartsWith("HTTP/1.1 200")) //继续读取内容 { int start = header .ToUpper().IndexOf("CONTENT-LENGTH"); int content_length = 0; if (start > 0) { string temp = header.Substring(start, header.Length - start); string[] sArry = Regex.Split(temp, "\r\n"); content_length = Convert.ToInt32(sArry[0].Split(':')[1]); if (content_length > 0) { byte[] bytes = new byte[content_length]; if (ssl.Read(bytes, 0, bytes.Length) > 0) { return bytes; } } } else { //不存在Content-Length协议头 return ParseSslResponse(ssl); } } else { return Encoding.Default.GetBytes(header); } } else { source.Cancel(); //超时的话,别忘记取消任务哦 } return null; }
static byte[] ParseHttpArgs(HttpMethod method, HttpArgs args) { StringBuilder bulider = new StringBuilder(); if (method.Equals(HttpMethod.POST)) { bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url)); bulider.AppendLine("Content-Type: application/x-www-form-urlencoded"); } else { bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url)); } bulider.AppendLine(string.Format("Host: {0}", args.Host)); bulider.AppendLine("User-Agent: Mozilla/5.0 (Windows NT 6.1; IE 9.0)"); if (!string.IsNullOrEmpty(args.Referer)) bulider.AppendLine(string.Format("Referer: {0}", args.Referer)); bulider.AppendLine("Connection: keep-alive"); bulider.AppendLine(string.Format("Accept: {0}", args.Accept)); bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie)); if (method.Equals(HttpMethod.POST)) { bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Body).Length)); bulider.Append(args.Body); } else { bulider.Append("\r\n"); } string header = bulider.ToString(); return Encoding.Default.GetBytes(header); }
static byte[] InternalSslSocketHttp(IPEndPoint endpoint, X509CertificateCollection certificates, HttpArgs args, HttpMethod method) { TcpClient tcp = new TcpClient(); try { tcp.Connect(endpoint); if (tcp.Connected) { using (SslStream ssl = new SslStream(tcp.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null)) { ssl.AuthenticateAsClient("ServerName", certificates, SslProtocols.Tls, false); if (ssl.IsAuthenticated) { byte[] buff = ParseHttpArgs(method, args); //生成协议包 ssl.Write(buff); ssl.Flush(); return ParseSslResponse(endpoint, ssl, args, certificates); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return null; }
static byte[] InternalSocketHttp(IPEndPoint endpoint, HttpArgs args, HttpMethod method) { using (Socket sK = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { sK.Connect(endpoint); if (sK.Connected) { byte[] buff = ParseHttpArgs(method, args); if (sK.Send(buff) > 0) { return ParseResponse(endpoint, sK, args); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return null; }