/// <summary> /// 设置此类的字段 /// </summary> /// <param name="headText">头部文本</param> private static void SetThisHeaders(string headText) { if (string.IsNullOrWhiteSpace(headText)) { throw new ArgumentNullException("headText"); } string[] headers = headText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); if (headers == null || headers.Length == 0) { throw new ArgumentException("'WithHeadersText' param format error."); } HttpHeaders = new HttpHeader(); foreach (string head in headers) { if (head.StartsWith("HTTP", StringComparison.OrdinalIgnoreCase)) { string[] ts = head.Split(' '); if (ts.Length > 1) { HttpHeaders.ResponseStatusCode = ts[1]; } } else if (head.StartsWith("Set-Cookie:", StringComparison.OrdinalIgnoreCase)) { Cookies = Cookies ?? new List <string>(); string tCookie = head.Substring(11, head.IndexOf(";") < 0 ? head.Length - 11 : head.IndexOf(";") - 10).Trim(); if (!Cookies.Exists(f => f.Split('=')[0] == tCookie.Split('=')[0])) { Cookies.Add(tCookie); } } else if (head.StartsWith("Location:", StringComparison.OrdinalIgnoreCase)) { HttpHeaders.Location = head.Substring(9).Trim(); } else if (head.StartsWith("Content-Encoding:", StringComparison.OrdinalIgnoreCase)) { if (head.IndexOf("gzip", StringComparison.OrdinalIgnoreCase) >= 0) { HttpHeaders.IsGzip = true; } } else if (head.StartsWith("Content-Type:", StringComparison.OrdinalIgnoreCase)) { string[] types = head.Substring(13).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string t in types) { if (t.IndexOf("charset=", StringComparison.OrdinalIgnoreCase) >= 0) { HttpHeaders.Charset = t.Trim().Substring(8); } else if (t.IndexOf('/') >= 0) { HttpHeaders.ContentType = t.Trim(); } } } else if (head.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) { HttpHeaders.ContentLength = long.Parse(head.Substring(15).Trim()); } else if (head.StartsWith("Transfer-Encoding:", StringComparison.OrdinalIgnoreCase) && head.EndsWith("chunked", StringComparison.OrdinalIgnoreCase)) { HttpHeaders.IsChunk = true; } } }