0
嗨我在C#中實現了下面的代碼來捕獲標題屬性。 出於某種原因,我只獲取名字的數據,其餘字段顯示爲空。我知道他們有價值,但由於某種原因,他們顯示爲空。捕獲http標題屬性
// Load ServerVariable collection into NameValueCollection object.
System.Collections.Specialized.NameValueCollection headerdetails = Request.Headers;
List<string> requiredHeaders = new List<string>();
requiredHeaders.Add("FirstName");
requiredHeaders.Add("MiddleName");
requiredHeaders.Add("LastName");
requiredHeaders.Add("email");
// Get names of all keys into a string array.
String[] arr1 = headerdetails.AllKeys;
for (int i = 0; i < arr1.Length; i++)
{
if (requiredHeaders.Contains(arr1[i]))
{
if (arr1[i] == "FirstName")
{
String[] arr2 = headerdetails.GetValues(arr1[i]);
for (int values = 0; values < arr2.Length; values++)
{
string firstname = Server.HtmlEncode(arr2[values]);
Session["firstName"] = firstname;
}
}
if (arr1[i] == "MiddleName")
{
String[] arr2 = headerdetails.GetValues(arr1[i]);
for (int values = 0; values < arr2.Length; values++)
{
string middlename = Server.HtmlEncode(arr2[values]);
Session["middleName"] = middlename;
}
}
if (arr1[i] == "LastName")
{
String[] arr2 = headerdetails.GetValues(arr1[i]);
for (int values = 0; values < arr2.Length; values++)
{
string lastname = Server.HtmlEncode(arr2[values]);
Session["lastName"] = lastname;
}
}
if (arr1[i] == "email")
{
String[] arr2 = headerdetails.GetValues(arr1[i]);
for (int values = 0; values < arr2.Length; values++)
{
string email = Server.HtmlEncode(arr2[values]);
Session["emailID"] = email;
}
}
您需要訪問您的網站的客戶這些標題?爲什麼?這些不是標準標題,你有什麼機制來確保客戶發送這些信息?對於實際的問題,你做了什麼調試?您是否確保客戶端在請求中發送這些頭文件?當你通過這個代碼服務器端時,它出現了哪些問題? – David
是的客戶端發送這些頭。我也讓他們在我的日誌中被捕獲。 –