在通用处理程序页面中使用asp.net获取域名

时间:2011-08-29 19:04:53

标签: asp.net vb.net

我想获取远程ip的域名。我有两个域名(网站)。例如www.a1.com和www.a2.com。在a2域中向a1域名的页面发送请求,如GetRequest.ashx

http请求的示例是

http://www.a1.com/GetRequest.ashx?username=bala&password=123456

在我的GetRequest.ashx页面示例编码

<%@ WebHandler Language="VB" Class="Handler" %>

Imports System
Imports System.Web

Public Class GetRequest : Implements IHttpHandler

   Public Sub ProcessRequest(ByVal context As HttpContext) Implements     IHttpHandler.ProcessRequest

          context.Response.ContentType = "text/plain"
          Dim username As String = context.Request.QueryString("username")
          Dim password As String = context.Request.QueryString("password")
 **'//Here i need a coding to get requested domain name that is who send the request to my page**

   End Sub

   Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
       Get
         Return False
      End Get
  End Property
End Class

我已经使用了以下编码,但没有解决我的问题。因为它返回ip地址。我只需要域名而不是ip。

context.Request.ServerVariables("REMOTE_ADDR")
context.Request.ServerVariables("REMOTE_HOST")

Dim domain As String
Dim url As Uri = HttpContext.Current.Request.Url
domain = url.AbsoluteUri.Replace(url.PathAndQuery, String.Empty)

变量域包含www.a1.com但我需要www.a2.com

使用google analytics api来解决我的问题?那么如何使用这个api可以解释任何一个

3 个答案:

答案 0 :(得分:2)

Page.Request.Url.Host包含网址的主机名(示例中为www.a1.com

如果www.a2.com站点上的请求调用www.a1.com站点上的页面,则主机名将始终为www.a1.com,因为这是用于调用该页面的主机。如果您需要知道请求来自www.a2.com

,我建议传递查询字符串变量

答案 1 :(得分:0)

检查推荐人:

HttpContext.Current.Request.UrlReferrer.Host

在您的代码中:

   Public Sub ProcessRequest(ByVal context As HttpContext) Implements     IHttpHandler.ProcessRequest

          context.Response.ContentType = "text/plain"
          Dim username As String = context.Request.QueryString("username")
          Dim password As String = context.Request.QueryString("password")
 **'//Here i need a coding to get requested domain name that is who send the request to my page**
          Dim domain as string = context.Request.UrlReferrer.Host

   End Sub

答案 2 :(得分:0)

您可以通过HttpContext访问请求对象,如下所示:

编辑:已更改为获取引荐网址的主机名

string host = HttpContext.Current.Request.UrlReferrer.Host;

编辑:UrlReferrer返回null。替代使用HTTP_REFERER:

if (!String.IsNullOrEmpty(Request.ServerVariables["HTTP_REFERER"]))
{
    Uri referringUrl = new Uri(Request.ServerVariables["HTTP_REFERER"]);
    string referringHostName = referringUrl .Host;
}