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

时间:2011-12-16 20:52:27

标签: asp.net vb.net httpwebrequest

我正在使用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地址。

3 个答案:

答案 0 :(得分:0)

这应该做你想要的。它将对网站的主机名进行DNS查找,该主机名应该为您提供正在侦听的IP地址。

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

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

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

答案 1 :(得分:0)

根据您目前所说的内容,您要查找的变量是:

Request.UserHostAddress
Request.UserHostName

问题是,如果他们使用代理,您可能无法获得唯一的IP,但除此之外,您应该能够从他们那里获得所需的内容。

答案 2 :(得分:0)

现在更多地了解你的问题......

我认为您无法获得连接到您网站的其他网站。从技术上讲,与您的网站的唯一连接是连接到您的网站,因此,除非他们可以通过用户的IP地址,否则您将无法接触到该