从Header中获得信息


/// <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; }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM