Asp.NET MVC REST API: how to get client host name

I am developing a REST API application in ASP.NET MVC3. I am trying to get the HOST client name in action but cannot succeed. I use this API in several applications, so I want to register a domain name.

Link 1

 HttpContext.Current.Request.UserHostAddress;
 System.Web.HttpContext.Current.Request.UserHostName;
 System.Web.HttpContext.Current.Request.UserHostAddress;

Link 2

private string GetClientIp(HttpRequestMessage request)
{
    if (request.Properties.ContainsKey("MS_HttpContext"))
    {
        return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
    }
    else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
    {
        RemoteEndpointMessageProperty prop;
        prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
        return prop.Address;
    }
    else
    {
        return null;
    }
}

All of these options provide an IP address, but I want to get the host name

API controller www.myapi.com

public class TESTController : ApiController
{
    public TESTController()
    {
    }

    [HttpGet]
    public object Add(string data = "")
    {
        try
        {
            string result = "0";

            if (!string.IsNullOrEmpty(data))
            {
                //should be www.myapiconsume.com
                var host = System.Web.HttpContext.Current.Request.UserHostName;
                var ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            }

            return new { response = result };
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}

Calling an API action from another domain www.myapiconsume.com

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var url = "http://myapi.com/api/test/add?data={0}";
            url = string.Format(url, "{ data}");
            WebClient client = new WebClient();
            var result = client.DownloadString(url);
            return Content(result);
        }

    }

How can i get this?

+4
source share
3 answers

It is not provided by default, unfortunately; if you are on the same network, you can try:

var result = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables("remote_addr"));

, ..

+1

:

var url = "http://myapi.com/api/test/add?data={0}";
url = string.Format(url, "{ data}");
WebClient client = new WebClient();
client.Headers.Add("Referer", Request.Url.AbsoluteUri);
var result = client.DownloadString(url);
return Content(result);

:

Request.UrlReferrer.Host

IP-, :

Dns.GetHostEntry(HttpContext.Current.Request.UserHostAddress)
+1

You can use the Dns.GetHostEntry Reverse Lookup Method.

public static string ReverseLookup(string ip)
{
    if (String.IsNullOrWhiteSpace(ip))
        return ip;

    try
    {
        IPHostEntry host = Dns.GetHostEntry(ip);

        return host != null ? host.HostName : ip;
    }
    catch (SocketException)
    {
        return ip;
    }
}
+1
source

All Articles