使用 ASP.NET 获取网站专用 IP 地址

发布于 2024-12-21 12:56:03 字数 1359 浏览 1 评论 0原文

我正在使用 HttpWebRequest 进行通信。可以获取网站IP地址(专用IP)而不是服务器IP。 当我使用 Context.Request.ServerVariable("Remote_Addr") 时,它仅返回服务器 IP。 但我需要网站ip地址。

例如,

有 3 个客户的网站向我的网站发送 httpwebrequest。每个都有专用的IP地址。

我的网站接收该请求并执行一些工作,然后做出响应。

客户端发送 HttpWebRequest 编码示例:

Dim uri As New Uri("http://www.somewebsite.com/somepage.ashx?username=client1&password=123456")
If (uri.Scheme = uri.UriSchemeHttp) Then
    Dim wrequest As HttpWebRequest = HttpWebRequest.Create(uri)
    wrequest.Method = WebRequestMethods.Http.Get
    Dim wresponse As HttpWebResponse = wrequest.GetResponse()     
    Dim reader As New StreamReader(wresponse.GetResponseStream())
    Dim tmp As String = reader.ReadToEnd()
wresponse.Close()  
End If

HttpHandler 编码示例:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
 Dim remoteIP As String = context.Request.ServerVariables("REMOTE_ADDR")
 If remoteIP = "client1ip" Then
    Dim ref As String = Trim(context.Request.QueryString("username"))
    Dim number As String = Trim(context.Request.QueryString("password"))
   'do some work
Else
    context.Response.Write("Access Denied")
End If

End Sub

这是我的示例编码。这里remoteIP返回托管提供商服务器IP,但我需要网站IP地址。

可以使用 httphandler 中的 httpcontext 获取网站 IP 地址。

I am Useing HttpWebRequest for communications. It is possible to get website ip address(dedicated ip) not server ip.
when i use Context.Request.ServerVariable("Remote_Addr") it return only server ip.
But I need website ip address.

For Example

There are 3 client's website send httpwebrequest to my site. Each have dedicated ip address.

My Website Receive that Request and perform some work and then response.

Example Coding Client Send HttpWebRequest:

Dim uri As New Uri("http://www.somewebsite.com/somepage.ashx?username=client1&password=123456")
If (uri.Scheme = uri.UriSchemeHttp) Then
    Dim wrequest As HttpWebRequest = HttpWebRequest.Create(uri)
    wrequest.Method = WebRequestMethods.Http.Get
    Dim wresponse As HttpWebResponse = wrequest.GetResponse()     
    Dim reader As New StreamReader(wresponse.GetResponseStream())
    Dim tmp As String = reader.ReadToEnd()
wresponse.Close()  
End If

Example Coding For HttpHandler:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
 Dim remoteIP As String = context.Request.ServerVariables("REMOTE_ADDR")
 If remoteIP = "client1ip" Then
    Dim ref As String = Trim(context.Request.QueryString("username"))
    Dim number As String = Trim(context.Request.QueryString("password"))
   'do some work
Else
    context.Response.Write("Access Denied")
End If

End Sub

This is my example coding. Here remoteIP return hosting provider server ip but i need website ip address.

it is possible to get website ip address using httpcontext in httphandler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

触ぅ动初心 2024-12-28 12:56:03

这应该可以满足您的要求。它将对网站的主机名进行 DNS 查找,这将为您提供它正在侦听的 IP 地址。

string dnsHostname = "yourwebsite.com";
IPAddress[] addresslist = Dns.GetHostAddresses(dnsHostname);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}

您可能只需要数组的第一个结果。

This should do what you're looking for. It will do a DNS lookup on the hostname of the website, which should give you the IP Address that it is listening on.

string dnsHostname = "yourwebsite.com";
IPAddress[] addresslist = Dns.GetHostAddresses(dnsHostname);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}

You will likely only need the first result of the array.

甜是你 2024-12-28 12:56:03

根据您到目前为止所说的,您正在寻找的变量是:

Request.UserHostAddress
Request.UserHostName

您将遇到的问题是,如果他们使用代理,您可能无法获得唯一的 IP,但除此之外,您应该能够从他们那里得到你需要的东西。

Based on what you've said so far, the variables you're looking for are:

Request.UserHostAddress
Request.UserHostName

The issue you will be that you might not get a unique IP if they're using a proxy, but other than that, you should be able to get what you need from them.

听你说爱我 2024-12-28 12:56:03

现在更多地了解您的问题之后...

我认为您将无法让访问者访问连接到您网站的其他网站。从技术上讲,与您网站的唯一连接是连接到您的网站,因此除非他们可以传递用户的 IP 地址,否则您将不会接触到该网站

After understanding your question more now...

I don't think you are going to be able to get the vistor this other website that is connecting to your site. Technically the only connection to your website is the website that is connecting to you, so unless they can pass the user's ip address, you will have no exposure to that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文