public static MemoryStream GetFile(string url) { try { var data = new Http_Client().get(url); return(data); } catch (Exception ex) { Console.WriteLine(string.Format("url:{0} , error:{1}", url, ex.Message)); return(null); } }
private MemoryStream init(string url, string datainfo = null, string method = "get", string charset = "UTF-8", string referer = null) { MemoryStream stream = null; #region var coding = Encoding.GetEncoding(charset); byte[] dataByte = null; if (datainfo != null) { dataByte = coding.GetBytes(datainfo); } Uri uri = new Uri(url); _socket.Connect(uri.Host, uri.Port); MemoryStream ms = new MemoryStream(); if (referer == null) { referer = url; } StringBuilder sb = new StringBuilder(); sb.Append(string.Format("{0} {1} HTTP/1.1\r\n", method.ToUpper(), uri.PathAndQuery)); sb.Append(string.Format("Host: {0}\r\n", uri.Authority)); sb.Append("Accept: */*\r\n"); sb.Append("Content-Type:application/x-www-form-urlencoded\r\n"); //sb.Append("Connection: keep - alive\r\n"); //sb.Append("User - Agent: Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 63.0.3239.132 Safari / 537.36\r\n"); //sb.Append(string.Format("Accept: image / webp,image / apng,image/*,*/*; q = 0.8\r\n")); //sb.Append(string.Format("Referer: {0}\r\n", referer)); //sb.Append(string.Format("Accept - Encoding: gzip, deflate, br\r\n")); //if (datainfo != null) // sb.Append(string.Format("Content-Length:{0}\r\n", dataByte.Length)); //sb.Append(string.Format("Accept - Language: zh - CN,zh; q = 0.9,en; q = 0.8\r\n")); sb.Append(string.Format("\r\n")); var sendinfo = sb.ToString(); _socket.ReceiveTimeout = 1000 * timeout; _socket.SendTimeout = 1000 * timeout; isHttps = string.Compare(uri.Scheme, "https", false) == 0; if (isHttps) { ssl = new SslStream(new NetworkStream(_socket), false, delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(sslPolicyErrors == SslPolicyErrors.None); }, null); ssl.ReadTimeout = 1000 * timeout; ssl.WriteTimeout = 1000 * timeout; ssl.AuthenticateAsClient(uri.Host); ssl.Write(coding.GetBytes(sendinfo)); if (datainfo != null) { ssl.Write(dataByte); } //ssl.Flush(); } else { var result_int = _socket.Send(coding.GetBytes(sendinfo)); if (datainfo != null) { result_int = _socket.Send(dataByte); } } #endregion byte[] filedata = getHeader(); int status_code = int.Parse(HttpHeaders.ResponseStatusCode); if (status_code != 200) { } switch (status_code) { case 403: return(stream); default: break; } //Console.WriteLine(" {0}",status_code); if (status_code >= 500) { Console.WriteLine(" 服务器返回状态码:{0} --- {1}", status_code, url); return(stream); } if (HttpHeaders.IsChunk) { byte[] data = new byte[102400]; int bodyStart = 0;//数据部分起始位置 int readLength = filedata.Length; stream = new MemoryStream(); Array.Copy(filedata, 0, data, 0, filedata.Length); GetChunkData(ref data, ref bodyStart, ref readLength, ref stream); } else { stream = defaultReceive(filedata); } if (!string.IsNullOrWhiteSpace(this.HttpHeaders.Location)) { stream = new Http_Client().get(this.HttpHeaders.Location, referer = uri.AbsoluteUri); } if (HttpHeaders.IsGzip) { stream = unGzip(stream); } stream.Flush(); return(stream); }