查找客户计算机的IP地址 [英] find ip address of client's computer

查看:83
本文介绍了查找客户计算机的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#在asp.net中正确找到客户端计算机的IP地址的代码是什么?

what code right in asp.net with C# to find the ip address of client''s computer?

推荐答案

using System;
using System.Web;

namespace WebApplication4
{
    public class Global : HttpApplication
    {
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Get request.
        HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        // Write to response.
        base.Response.Write(address);

        // Done.
        base.CompleteRequest();
    }
    }
}


尝试以下操作:

Try this one:

public string getclientIPAddress()
{
   string result = string.Empty;
   string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(ip))
            {
                string[] ipRange = ip.Split(',');
                int le = ipRange.Length - 1;
                result = ipRange[0];
            }
            else
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            return result;
        }




如果这解决了您的问题,请标记为答案并投票5

问候,
Eduard




Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard


这是Context.Request.UserHostAddress,其中Context具有类型System.Web.HttpContext,可以找到为System.Web.HttpContext.Current,或者可以通过页面的Context属性访问它System.Web.UI.Page,如以下MDSN帮助页面的第一页代码示例中所示:
http://msdn.microsoft.com/zh-我们/library/system.web.httpcontext%28v=VS.100%29.aspx [ http://msdn.microsoft.com/en-us/library/system. web.ui.page.aspx [ ^ ].

请记住,在大多数情况下,它是动态IP地址,它不能唯一地标识客户端计算机.

—SA
This is Context.Request.UserHostAddress, where Context has the type System.Web.HttpContext and can be found as System.Web.HttpContext.Current or you can accesse it via the Context property of your page System.Web.UI.Page, as it is shown in the code sample in the first of MDSN help pages references below:
http://msdn.microsoft.com/en-us/library/system.web.httpcontext%28v=VS.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx[^].

Keep in mind that in most cases it will be dynamic IP address which does not uniquely identify the client computer.

—SA


这篇关于查找客户计算机的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆