How can I get an IP address, given a domain name?
For example: www.test.com
You can use the System.Net.Dns class:
Dns.GetHostAddresses("www.test.com");
You could use the GetHostAddresses method:
var address = Dns.GetHostAddresses("www.test.com")[0];
You can get the same results by using:
Dns.GetHostAddresses("yahoo.com");
or
await Dns.GetHostAddressesAsync("yahoo.com");
My answer could be more the same above answers, but here i get the current web app hosted URL / domain name using code and obtained the IP address and from that. I used the code in my C# MVC Web app and its working fine.
Uri myUri = new Uri(((System.Web.HttpContextWrapper)HttpContext).Request.Url.ToString());
var ipAddress = Dns.GetHostAddresses(myUri.Host).FirstOrDefault().ToString();
Related
I am currently trying to create a valid link to a function of my API using this LOC:
Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
This returns the following:
http://localhost:53800/..../user/821105b1
However, as the link should be accessible from the network, I would need something like the following:
http://192.168.0.12:53800/..../user/821105b1
How can I get this result instead of the one with the localhost??
Thanks in advance!
Calling local libraries will only give you the local IP. You can call an external API like whatismyip or checkip to get the external IP. See here...https://stackoverflow.com/a/7838551
I have a simple webservice which returns a string of message as below:
[WebMethod]
public string GetData()
{
return "Here is your response";
}
So just to log details, is it possible to identify from which system the service has been invoked?
You can use HttpContext by:
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
And there is more usefull informations.
You can get the host address from the OperationContext.Current. The below code would do it.
RemoteEndpointMessageProperty endpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string address = endpoint.Address;
This will work for both self hosted and IIS hosted services. The Address might give a IPV6 address when the network is configured with it. So you may need to resolve using Dns.GetHostEntry(address) to get the IPV4 address.
I have been trying to navigate my webview element in my windows store app to '192.168.0.1' but for some reason the Uri class can not parse it, is there a way to make convert a IP Address into a Uri?
You can also use UriBuilder and not have to manually add the "http://".
var builder = new UriBuilder("192.168.0.1");
var uri = builder.Uri;
The answer to this is to add the prefix of the ip's protocol:
http:// or https://, for instance
new Uri("192.168.0.1") would have to be new Uri("http://192.168.0.1/")
Thanks to Bob Kaufman
I have an ASP.NET 3.5 Web application with C# 2008.
What I need to do is, I want to extract full domain name in side a class method from Current URL.
For example :
I do have Current URL like :
http://subdomain.domain.com/pagename.aspx
OR
https://subdomain.domain.com/pagename.aspx?param=value¶m2=value2
Then the result should be like,
http://subdomain.domain.com
OR
https://subdomain.domain.com
You're looking for the Uri class:
new Uri(str).GetLeftPart(UriPartial.Authority)
Create a Uri and query the Host property:
var uri = new Uri(str);
var host = uri.Host;
(Later)
I just realized that you want the scheme and the domain. In that case, #SLaks answer is the one you want. You could do it by combining the uri.Scheme and uri.Host, but that can get messy for things like mailto urls, etc.
Out customers can enter websites from domain names. They also can enter mailadresses from their contacts.
Know we need to find customers which websited whoose domain can be associated to the domains of the mailadresses.
So my idea is to extract the host from the webadress and from the url and compare them
So what's the most reliable algorithm to get the hostname from a url?
for example a host can be:
foo.com
www.foo.com
http://foo.com
https://foo.com
https://www.foo.com
The result should always be foo.com
Rather than relying on unreliable regex use System.Uri to do the parsing for you. Use a code like this:
string uriStr = "www.foo.com";
if (!uriStr.Contains(Uri.SchemeDelimiter)) {
uriStr = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, uriStr);
}
Uri uri = new Uri(uriStr);
string domain = uri.Host; // will return www.foo.com
Now to get just the top-level domain you can use:
string tld = uri.GetLeftPart( UriPartial.Authority ); // will return foo.com
Here's a regular expression that will match the url's you have provided. Basically http and https etc are optional, as is the www Everything is then matched up to a possible path;
var expression = /(https?:\/\/)?(www\.)?([^\/]*)(\/.*)?$/;
This would mean that;
var result = 'https://www.foo.com.vu/blah'.replace(expression, '$3')
Would evaluate to
result === 'foo.com.vu'
There is already a url parser in c# for extracting this information
Here are some examples http://www.stev.org/post/2011/06/27/C-HowTo-Parse-a-URL.aspx
See this url. The Host property, unlike the Authority will not include the port number.
http://msdn.microsoft.com/en-us/library/system.uri.host(v=vs.110).aspx