主要使用了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>");
}
}
转载于:https://blog.51cto.com/leyton/379600