主要使用了C#中的HttpContext類,通過下面兩種方法都可以獲得來源URL。
string user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"];
Response.Write(user_IP);
string user_IP2 = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
Response.Write(user_IP2);
紅色的更好一些,如果客戶端沒有來源URL的時候,紅色顯示為空,而藍色則會直接報錯(我這里沒有加上是否為空的判斷)。
HttpContext還可以檢測許多別的東西,大家可以用下面的程序把它輸出出來,看看都是有什么;)
int loop1, loop2;
System.Collections.Specialized.NameValueCollection coll;
// Load ServerVariable collection into NameValueCollection object.
coll = Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}?
該文章在 2026/3/2 9:22:14 編輯過