复制代码 代码如下:
string user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e){
StringBuilder info = new StringBuilder(); // 我们关注的三个 ServerVariables
info.AppendFormat("HTTP_VIA = {0} <br />/r/n",
Request.ServerVariables["HTTP_VIA"]);
info.AppendFormat("HTTP_X_FORWARDED_FOR = {0} <br />/r/n",
Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
info.AppendFormat("REMOTE_ADDR = {0} <br />/r/n",
Request.ServerVariables["REMOTE_ADDR"]);
info.AppendLine("*********** /r/n<br />");
// 其他有参考价值的 ServerVariables
foreach (string key in Request.ServerVariables.AllKeys){
info.AppendFormat("{0} = {1} <br />/r/n", key, Request.ServerVariables[key]);
}
Response.Clear();
Response.Write(info.ToString());
Response.End();
}
客户端发送请求的代码:
[code]
static void Main(string[] args){
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost:7867/MyTestWebSite/UserIP.aspx");
request.Headers.Add("REMOTE_ADDR", "192.168.5.88");
request.Headers.Add("VIA", "ghj1976");
request.Headers.Add("X_FORWARDED_FOR", "0.0.0.0");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string info = stream.ReadToEnd();
stream.Close();
response.Close();
request = null;
Console.Write(info);
Console.ReadLine();
}
新闻热点
疑难解答
图片精选