花一些時間尋找出的現成的解決方案後,我最終實現的擴展方法System.Net.WebHeaderCollection
:
public static class WebHeaderCollectionExtensions
{
public static ILookup<string, string> ToLookup(this WebHeaderCollection some)
{
List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
if (some.Count > 0)
{
string[] tempSplittedHeaders = null;
foreach (string headerName in some)
{
if (some[headerName].Contains(";,"))
{
tempSplittedHeaders = Regex.Split(some[headerName], ";,");
foreach (string splittedHeader in tempSplittedHeaders)
{
headers.Add(new KeyValuePair<string, string>(headerName, splittedHeader));
}
}
else
{
headers.Add(new KeyValuePair<string, string>(headerName, some[headerName]));
}
}
}
return headers.ToLookup(keySelector => keySelector.Key, elementSelector => elementSelector.Value);
}
}
感謝這個美好的擴展方法,我能轉換頭「收集到的查找,它允許重複鍵,並在一天結束的時候,做了一些處理,我得到的所有的HTTP頭的列表中分別:
string wholeCookie = WebOperationContext.Current.IncomingRequest.Headers.ToLookup()["Set-Cookie"].Single(cookie => cookie.Contains("[Cookie name]"));
我希望分享我的解決方案將是一個不錯的因爲我猜別人曾經或曾經是哈維在類似的情況下使用!
如果說到cookie,我相信HttpWebRequest.CookieContainer屬性就是您要查找的內容,更多內容請參閱:http://msdn.microsoft.com/en-us/library/dd920298(v=vs。 95).aspx – tpeczek
@tpeczek我試過這種方式,但沒有運氣。 –