2011-08-29 35 views
2

我想獲取域名而不是遠程ip。我有兩個域(網站)。例如www.a1.com和www.a2.com。在A2域發送請求至A1域的頁面一樣GetRequest.ashx在通用處理頁面中使用asp.net獲取域名

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

使用谷歌分析的API來解決我的問題?那麼如何使用這個API可以在任何一個解釋

+0

請告訴我你真的在傳輸線上傳遞純文本用戶名/密碼。 – Eonasdan

+0

只是例如 –

+0

好。我擔心所有的SO都會因爲那個而打你:) – Eonasdan

回答

2

Page.Request.Url.Host包含URL的主機名(www.a1.com在你的例子)

如果在www.a2.com站點的請求呼籲www.a1.com網站頁面時,主機會總是www.a1.com,因爲那是用來調用頁面的主機。如果您需要知道請求來自www.a2.com,我建議傳遞一個查詢字符串變量。

+0

我需要誰發送請求。不是我的網站名稱我需要www.a2.com –

+0

www.a2.com發送請求到www.a1.com,所以我需要www.a2.com名稱好。 –

+0

我重新讀你的問題,並得到了。看到我編輯的答案 –

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 
+0

對不起,我使用request.urlreferrer.host它返回錯誤 –

+0

什麼是錯誤?您應該檢查UrlReferrer是否爲空。 – Martin

+0

未設置對象實例的對象引用 –

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; 
} 
+0

對不起,這也是返回我的本地域名是www.a1.com,但我需要的請求發件人的域名是www.a2.com –

+0

@Balachandran庫馬爾:請參閱編輯的答案。 –

+0

以下錯誤發生「對象引用未設置爲對象的實例」 –