【问题标题】:Get public IP using DynDNS and WebRequest C#使用 DynDNS 和 WebRequest C# 获取公共 IP
【发布时间】:2012-05-15 17:40:28
【问题描述】:

我使用此代码获取公共 IP 地址(感谢此帖子 How to get the IP address of the server on which my C# application is running on?):

    public static string GetPublicIP()
    {
        try
        {
            String direction = "";
            WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                {
                    direction = stream.ReadToEnd();
                }
            }

            //Search for the ip in the html
            int first = direction.IndexOf("Address: ") + 9;
            int last = direction.LastIndexOf("</body>");
            direction = direction.Substring(first, last - first);

            return direction;
        }
        catch (Exception ex)
        {
            return "127.0.0.1";
        }
    }

但是无论谁访问我的网站,他们都获得相同的IP,并且是服务器公共IP,而不是当前用户的IP。

是否可以在当前用户的上下文中运行 WebRequest,而不是作为服务器?

或者是我在 App_Code 中运行这个函数导致当前用户请求不可用,而是使用服务器上下文的问题?

请帮忙!

【问题讨论】:

  • 网站调用/请求是从您的服务器发生的。这意味着在运行此代码的服务器发送的 HTTP 数据包中,它将包含您的服务器 ipaddress。因此,如果您想要客户的 IP 地址,那么您为什么要请求其他网站?只需打开来自客户端的 http 数据包。希望我对您的问题的理解是正确的。 :O
  • 猜你是对的,但如果我使用 ex,我不会得到正确的 IP。 Request.ServerVariables["REMOTE_ADDR"]。然后我得到我的本地 VPN IP,而不是我使用 checkip.dyndns.org 时得到的公共 IP。
  • 如果涉及 VPN,您想要的可能无法实现。您能否举例说明您在特定情况下会发生什么?
  • 我假设您已经运行了一个 Web 服务。您必须让 WCF 托管您的服务。我想我不确定你的端到端故事是什么。您可以通过此tutorial 开始使用 WCF 服务
  • 是否有任何商业图书馆或可用的东西? checkip.dyndns.org 如何提供正确的地址?

标签: c# ip dyndns


【解决方案1】:

由于您是从服务器发出请求,因此您将获得服务器的 IP 地址。

我不确定你有什么样的服务。对于 WCF 服务,您可以从请求的 OperationContext 对象的 IncomingMessageProperties 中获取 IP 地址。在此处查看示例:Get the Client’s Address in WCF

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string GetAddressAsString();
}

public class MyService : IMyService
{
    public string GetAddressAsString()
    {
        RemoteEndpointMessageProperty clientEndpoint =
            OperationContext.Current.IncomingMessageProperties[
            RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

        return String.Format(
            "{0}:{1}",
            clientEndpoint.Address, clientEndpoint.Port);
    }
}

【讨论】:

  • 谢谢,但我得到 Object reference not set to an object of an object on RemoteEndpointMessageProperty clientEndpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
  • 我是这样实现的吧? IMyService s = new MyService();字符串 ip = s.GetAddressAsString();
  • 我假设您已经运行了一个 Web 服务。您必须让 WCF 托管您的服务。我想我不确定你的端到端故事是什么。您可以通过此tutorial 开始使用 WCF 服务
  • 我现在已经成功实现了 WCF 服务,但是它返回 127.0.0.1 作为我的 IP。
  • 非常好。现在,只是为了确保,您是在一台与您的服务托管位置不同的机器上进行测试的,对吧?如果没有,那么看看你是否可以从另一台机器调用你的服务来测试这个。
【解决方案2】:

我猜这段代码是在网络服务器上运行的——你有一个页面可以让客户检查他们的 IP 地址吗?如果是这样,我怀疑你已经糊涂了。如果不是,请详细说明它在哪里运行。

如果上面的代码在服务器上运行,那么如果你调用远程服务器并询问“这个请求的IP地址是什么”,你总是会得到该服务器的公共IP地址from" - 这是您的代码示例正在执行的操作。

如果您想要呼叫您的客户端的 IP 地址 - 假设您是一个 Web 应用程序,那么您应该查看 HttpWebRequest.UserHostAddress,尽管请注意这不是万无一失的。查看here 了解更多信息。

【讨论】:

  • 猜我有点困惑。现在我通过 VPN 连接到互联网,我的用户也可能通过 VPN 连接。因此,如果我尝试使用 HttpWebRequest.UserHostAddress,我会得到我的本地 VPN IP 地址,而不是访问checkip.dyndns.org 时获得的公共地址。
【解决方案3】:

这应该会发生,代码正在您的机器上运行,因此您可以获得自己的 IP 地址。要从用户那里获取信息,您必须检查用户发送的标头,特别是 REMOTE_ADDR 标头。

您可能可以在代码中的某处使用 Request.ServerVariables["REMOTE_ADDR"]

【讨论】:

  • 这将返回我的本地 VPN IP,而不是我使用 checkip.dyndns.org 时获得的公共 IP。
猜你喜欢
  • 1970-01-01
  • 2020-06-06
  • 2012-05-08
  • 2012-03-17
  • 2011-05-14
  • 1970-01-01
  • 2018-10-22
  • 2016-05-27
  • 2011-09-11
相关资源
最近更新 更多