IP to URI c#

DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
Does any one know the answer to this.


a uri is in the format Google or ftp://ftp.google.com for example

But I have the IP address 8.8.8.8 (yer google PublicDNS server)

now I could do a recursive look up to get the url for this, but is there a uri format for IP address

something like

http://8.8.8.8/

?

I have a program that runs test against URL, but I realised it does not work if the user puts in the native IP, so my options are to

rewrite it to natively handle IP's
use a recursive step to obtain the Uri (but this assume the IP has a URL name at all)
or work out how to make the IP with as a URL.
Uri testuri = new Uri("http:\\8.8.8.8");

this tells me it is incorrectly formatted.
  • If you can't explain it simply, you don't understand it well enough. Albert Einstein
  • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.

Comments

  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    DevilWAH wrote: »
    Does any one know the answer to this.


    a uri is in the format Google or ftp://ftp.google.com for example

    But I have the IP address 8.8.8.8 (yer google PublicDNS server)

    now I could do a recursive look up to get the url for this, but is there a uri format for IP address

    something like

    http://8.8.8.8/

    ?

    I have a program that runs test against URL, but I realised it does not work if the user puts in the native IP, so my options are to

    rewrite it to natively handle IP's
    use a recursive step to obtain the Uri (but this assume the IP has a URL name at all)
    or work out how to make the IP with as a URL.
    Uri testuri = new Uri("http:\\8.8.8.8");
    

    this tells me it is incorrectly formatted.

    Well, you are going to want to go hide your face in shame when I point this out! LOL! Your slashes are the wrong ones. The first one is escaping the second so you are not getting an error but the string you are building is http:\8.8.8.8 which is an invalid format! http://8.8.8.8 is a valid URI.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    ah ha, only in my post here :)

    in my code its the correct way, and still don't like it icon_sad.gif
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Well, it's a valid format for me... .NET 4.0.
    static void Main(string[] args)
            {
                try
                {
                    //Test the string.
                    Uri uri = new Uri("http://8.8.8.8");
                    Console.WriteLine("It worked");
                    Console.WriteLine(uri.AbsoluteUri);
                    Ping ping = new Ping();
                    PingReply reply = ping.Send(uri.DnsSafeHost);
                    if (reply.Status == IPStatus.Success)
                    {
                        Console.WriteLine("Address: {0}", reply.Address.ToString());
                        Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                        Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                        Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                        Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                    }
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("It failed.");
                }
                Console.ReadLine();
            }
    
    It worked
    http://8.8.8.8/
    Address: 8.8.8.8
    RoundTrip time: 24
    Time to live: 53
    Don't fragment: False
    Buffer size: 32
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    strange I was using uri.host to pull out the host part and it throws an error saying it cant determine the host part and it is incorrectly formatted

    funny that's the identical code I had in my IDE already ;)

    "http://www.google.co.uk" works fine

    "http://8.8.8.8" it does not like.

    ah well might have another play later but for now I have enough to work with, I just wanted to experiment with multi threading apps :)
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Have you been to PluralSight yet? I know I have been spamming about them so much I can't recall who I have mentioned them too yet. But I know you would find the site very useful.
  • apr911apr911 Member Posts: 380 ■■■■□□□□□□
    The Horrors!

    Not only are you not using DNS, but you are hardcoding your IP address!

    That being said, using the IP address (8.8.8.8 in this case) as your URI will be the same as using the canonical name google-public-dns-a.google.com

    In fact, the canonical name and DNS are there for human purposes... As far as the machine is concerned its going to 8.8.8.8 whether you use the IP or domain name...

    The benefits of DNS in programming is when you move the site to a different IP you dont have to pour through your code changing every instance of 8.8.8.8 to the new IP... You just have to update the DNS record on you DNS server and tell it this domain now lives here and your application will start using the new address. Of course, you must own the domain/dns record in order to update it and if you hardcode the domain name and the domain name changes you still need to pour back over the code and change each instance to the new domain...

    Which is why you should never hardcode IPs or domains!
    Currently Working On: Openstack
    2020 Goals: AWS/Azure/GCP Certifications, F5 CSE Cloud, SCRUM, CISSP-ISSMP
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    apr911 wrote: »
    The Horrors!

    Not only are you not using DNS, but you are hardcoding your IP address!

    That being said, using the IP address (8.8.8.8 in this case) as your URI will be the same as using the canonical name google-public-dns-a.google.com

    In fact, the canonical name and DNS are there for human purposes... As far as the machine is concerned its going to 8.8.8.8 whether you use the IP or domain name...

    The benefits of DNS in programming is when you move the site to a different IP you dont have to pour through your code changing every instance of 8.8.8.8 to the new IP... You just have to update the DNS record on you DNS server and tell it this domain now lives here and your application will start using the new address. Of course, you must own the domain/dns record in order to update it and if you hardcode the domain name and the domain name changes you still need to pour back over the code and change each instance to the new domain...

    Which is why you should never hardcode IPs or domains!

    Apart from when your IP's aren't linked to DNS, management IP address and the likes which may or may not be resolved by DNS, many companies will not enter management info in to DNS for security reasons.

    PS of course you don't use hard coded address in code, I would not use hard coded URL/URI, either have a editable config file, or request the user to input them at run time. As I mentioned in my previous post "I want to EXPERMENT with multi threading in c#", the question was regarding an issue with the format of a variable, did you not conside that possible I was hard-coding the IP in this example to bypass any errors using a varible reassignment for debugging reasons.

    Having set up DNS for several enterprise level solution I am well aware of how it works and the do's and don't.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    apr911 wrote: »
    The Horrors!

    Not only are you not using DNS, but you are hardcoding your IP address!

    That being said, using the IP address (8.8.8.8 in this case) as your URI will be the same as using the canonical name google-public-dns-a.google.com

    In fact, the canonical name and DNS are there for human purposes... As far as the machine is concerned its going to 8.8.8.8 whether you use the IP or domain name...

    The benefits of DNS in programming is when you move the site to a different IP you dont have to pour through your code changing every instance of 8.8.8.8 to the new IP... You just have to update the DNS record on you DNS server and tell it this domain now lives here and your application will start using the new address. Of course, you must own the domain/dns record in order to update it and if you hardcode the domain name and the domain name changes you still need to pour back over the code and change each instance to the new domain...

    Which is why you should never hardcode IPs or domains!

    You are looking at some example code used to ask for assistance, just a step up from pseudo-code, and making a slew of assumptions.
Sign In or Register to comment.