一般在ASP.NET通常會使用
Request.ServerVariables[“REMOTE_ADDR”]來取得使用者的IP。

但遇到代理伺服器時,則IP會變成是代理伺服器的,這時我們必須使用另外的方式取得使用者的IP。

判所client端是否有設定代理伺服器

1
2
3
4
5
6
7
8
if (Request.ServerVariables["HTTP_VIA"] == null)
{
myIpAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
myIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}

取得QueryString

1
string s = Request.Url.Query;

UrlDecode 針對中文字進行Decode

1
string s = HttpUtility.UrlDecode(Request.Url.Query);