请求之间的区别是什么?UserHostAddress和Request.ServerVariables(“REMOTE_ADDR”).ToString()

时间:2021-11-24 02:07:09

Here I can use either of these 2 methods. What are the differences and which one should I use?

这里我可以使用这两种方法中的任何一种。有什么不同之处,我该用哪个呢?

Method 1:

方法1:

    string srUserIp = "";
    try
    {
        srUserIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    }
    catch
    {

    }

Method 2:

方法2:

    string srUserIp = "";
    try
    {
        srUserIp = Request.UserHostAddress.ToString();
    }
    catch
    {

    }

2 个解决方案

#1


53  

Short answer: The two are identical.

简而言之:两者是相同的。

Long answer: To determine the difference between the two use Reflector (or whatever disassembler you prefer).

长答案:用反射器(或你喜欢的任何反汇编器)来确定两者之间的区别。

The code for the HttpRequest.UserHostAddress property follows:

HttpRequest的代码。UserHostAddress属性如下:

public string UserHostAddress
{
    get
    {
        if (this._wr != null)
        {
            return this._wr.GetRemoteAddress();
        }
        return null;
    }
}

Note that it returns _wr.GetRemoteAddress(). The _wr variable is an instance of an HttpWorkerRequest object. There are different classes derived from HttpWorkerRequest and which one is used depends on whether you are using IIS 6, IIS 7 or beyond, and some other factors, but all of the ones you would be using in a web application have the same code for GetRemoteAddress(), namely:

注意,它返回_wr.GetRemoteAddress()。_wr变量是HttpWorkerRequest对象的实例。从HttpWorkerRequest派生出不同的类,使用哪个类取决于您是在使用IIS 6、IIS 7还是其他一些因素,但是您在web应用程序中使用的所有类都有相同的GetRemoteAddress()代码,即:

public override string GetRemoteAddress()
{
    return this.GetServerVariable("REMOTE_ADDR");
}

As you can see, GetRemoteAddress() simply returns the server variable REMOTE_ADDR.

如您所见,GetRemoteAddress()只返回服务器变量REMOTE_ADDR。

As far as which one to use, I'd suggest the UserHostAddress property since is doesn't rely on "magic strings."

至于使用哪一个,我建议UserHostAddress属性,因为is不依赖于“魔法字符串”。

Happy Programming

快乐的编程

#2


5  

There is no difference. They return exactly the same value. However, one is IntelliSense-friendly whereas the other is not.

没有区别。它们返回的值完全相同。然而,一个是智能的,而另一个不是。

#1


53  

Short answer: The two are identical.

简而言之:两者是相同的。

Long answer: To determine the difference between the two use Reflector (or whatever disassembler you prefer).

长答案:用反射器(或你喜欢的任何反汇编器)来确定两者之间的区别。

The code for the HttpRequest.UserHostAddress property follows:

HttpRequest的代码。UserHostAddress属性如下:

public string UserHostAddress
{
    get
    {
        if (this._wr != null)
        {
            return this._wr.GetRemoteAddress();
        }
        return null;
    }
}

Note that it returns _wr.GetRemoteAddress(). The _wr variable is an instance of an HttpWorkerRequest object. There are different classes derived from HttpWorkerRequest and which one is used depends on whether you are using IIS 6, IIS 7 or beyond, and some other factors, but all of the ones you would be using in a web application have the same code for GetRemoteAddress(), namely:

注意,它返回_wr.GetRemoteAddress()。_wr变量是HttpWorkerRequest对象的实例。从HttpWorkerRequest派生出不同的类,使用哪个类取决于您是在使用IIS 6、IIS 7还是其他一些因素,但是您在web应用程序中使用的所有类都有相同的GetRemoteAddress()代码,即:

public override string GetRemoteAddress()
{
    return this.GetServerVariable("REMOTE_ADDR");
}

As you can see, GetRemoteAddress() simply returns the server variable REMOTE_ADDR.

如您所见,GetRemoteAddress()只返回服务器变量REMOTE_ADDR。

As far as which one to use, I'd suggest the UserHostAddress property since is doesn't rely on "magic strings."

至于使用哪一个,我建议UserHostAddress属性,因为is不依赖于“魔法字符串”。

Happy Programming

快乐的编程

#2


5  

There is no difference. They return exactly the same value. However, one is IntelliSense-friendly whereas the other is not.

没有区别。它们返回的值完全相同。然而,一个是智能的,而另一个不是。