protected string TryDeflateDecoded(string data) { var compressedBuffer = HttpEncoding.GetBytes(data); // Expected (gzip/deflate magic bytes) // 0x1F, 0x8B, 0x08 // 31, 139, 8 // UTF8: Got // 0x1F, 0xEF, 0xBF, 0xBD, 0x08, 0x00.., 0x04 // 31, 239, 191, 189, 8, (5 times) 0, 4, 0 // ASCII: Got // 0x1F, 0x3F, 0x08, 0x00.., 0x04 // 31, 63, 8, (5 times) 0, 4, 0 // Got it, encoding needs to be iso-8859-1 WTF ... // Hacky but works: They strip the GZIP trailer.. so we skip the GZIP header (10 bytes) and just decompress the deflate data try { const int gzipHeaderSize = 10; var deflateBuffer = new byte[compressedBuffer.Length - gzipHeaderSize]; Buffer.BlockCopy(compressedBuffer, gzipHeaderSize, deflateBuffer, 0, deflateBuffer.Length); var decompressedBuffer = Ionic.Zlib.DeflateStream.UncompressBuffer(deflateBuffer); var decompressedData = HttpEncoding.GetString(decompressedBuffer); return(decompressedData); } catch { } return(string.Empty); }
/// <summary> /// GetHtmlByPost /// </summary> /// <param name="url"></param> /// <param name="postDataString"></param> /// <param name="proxy"></param> /// <returns></returns> public string GetHtmlByPost(string url, string postDataString, IWebProxy proxy = null) { var stringEmpty = string.Empty; var html = stringEmpty; //postDataString 转化为 postDataByte var postDataByte = HttpEncoding.GetBytes(postDataString); var httpWebRequest = WebRequest.Create(url) as HttpWebRequest; if (httpWebRequest == null) { return(stringEmpty); } httpWebRequest.Method = "POST"; httpWebRequest.UserAgent = UserAgent; httpWebRequest.MaximumAutomaticRedirections = MaximumAutomaticRedirections; httpWebRequest.Timeout = Timeout; httpWebRequest.AllowAutoRedirect = AllowAutoRedirect; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.Referer = Referer; httpWebRequest.ContentLength = postDataByte.Length; if (proxy != null) { httpWebRequest.Proxy = proxy; } if (!string.IsNullOrEmpty(Cookies)) { httpWebRequest.Headers.Add("Cookie", Cookies); } //httpWebRequest写入post数据 using (var inputStream = httpWebRequest.GetRequestStream()) { inputStream.Write(postDataByte, 0, postDataByte.Length); } var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; using (var outputStream = httpWebResponse?.GetResponseStream()) { if (outputStream != null) { using (var streamReader = new StreamReader(outputStream, HttpEncoding)) { html = streamReader.ReadToEnd(); } } } //更新cookies Cookies = GetFormatCookies(httpWebResponse?.Headers["set-cookie"] ?? Cookies); return(html); }