/// <summary> /// 获得header信息 /// </summary> /// <param name="response"></param> private void GetHeader(HttpResponseMessage response) { var headers = response.Headers.ToString().Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); Dictionary<string, string> dictionary = new Dictionary<string, string>(); foreach (var header in headers) { //必须规定劈分两个,否则Set-Cookie会遗失数据 string[] splitStr = header.Split(new char[] { ':' }, 2); var name = splitStr[0]; var value = splitStr[1].Trim(); dictionary.Add(name, value); } //HeaderData是自定义类对象,存储header信息 headerData = new HeaderData(); var cookiePairs = dictionary["Set-Cookie"]; headerData.YourKeyword = GetValueByRegex(cookiePairs, "your keyword"); } private static string GetValueByRegex(string cookieStr, string pattern) { Regex regex = new Regex($"{pattern}=(\\S+);"); Match match = regex.Match(cookieStr); var pair = match.Groups[0].Value; pair = pair.TrimEnd(';'); string[] keyAndValue = pair.Split('='); string value = keyAndValue[1]; return value; }