How to get IPaddess from given String - c#

I have String like this:
http://192.168.xx.xx/abc/abcd.php
and want to fetch only ipaddress from it.
The Expected output should be:
192.168.xx.xx
I can do this by splitting it by '/' but is there any easy way out.

Here, give this a try:
var url = "http://192.168.1.1/abc/abcd.php";
Uri uri = new Uri(url);
var ip = Dns.GetHostAddresses(uri.Host)[0];
Console.WriteLine(ip.ToString());
You will need System.Net for this.

Related

Cannot parse a correct URI

I have this link: www.axams-freizeitzentrum.com/ruifach-stadion.htm
when I try to create an Uri using this code:
var link = new Uri("www.axams-freizeitzentrum.com/ruifach-stadion.htm");
this will return
Invalid URI: The format of the URI could not be determined.
what is wrong?
Check possible reasons here: http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.100).aspx
You need to put the protocol prefix in front the address, i.e. in your case "http://"
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");
Uris need a scheme name.
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");

Use UriBuilder and construct httpRequest

I try to build the following uri
http://localhost:8080/TestService.svc/RunTest
I do it as following
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost:8080/TestService.svc";
uriBuilder.Path = String.Format("/{0}", "RunTest");
string address = uriBuilder.ToString()
//In debugger the address looks like http://[http://localhost:8080/TestService.svc]/RunTest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
The above generates an exception
Invalid URI: The hostname could not be parsed.
I`ll appreciate your help in solving the issue
When using the Uri builder you need to put the host, port & path as it's own line. Also TestService.svc is also part of the path and not the host, you can get away with it if not using port but with port they have to be separated out.
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost";
uriBuilder.Port = 8080;
uriBuilder.Path = String.Format("/{0}/{1}", "TestService.svc", "RunTest");
var address = uriBuilder.ToString();
When I run your code, I also see square brackets as the value for the address variable as you point out, but I don't see PerfTestService in the generated Uri, nor do I see why this would be?! I see:
http://[localhost:8080/TestService.svc]/RunTest
Since you already know the host and the path, I suggest you construct it as a string.
var uriBuilder = new UriBuilder("http://localhost:8080/TestService.svc/RunTest");
string address = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

Get current URL but excluding some parts

The current URL looks like: http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA
I need to get the part before the #, so it's gonna be: http://mycompany.com/jobs/java-developer.aspx
How can I do that in code behind?
You can use Uri class:
var uri = new Uri("http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA");
var result = uri.GetLeftPart(UriPartial.Path);
string url = "http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA";
string path = url.Substring(0, url.IndexOf("#"));
Uri myUri = new Uri("http://mycompany.com/jobs/java-developer.aspx#.U9-RH2OTLfA");
string url = myUri.Host + myUri.LocalPath;
you can create uri with string then get the parts
private String getGoodString(String url)
{
return url.Substring(0, url.IndexOf("#") - 1);
}
This will return everything up to but not including the # symbol in your string. Assuming you have a # to signal when to stop getting the string of the URL. Otherwise if there are multiple #. You can use lastIndexOf, or firstIndexOf if you need to "trim the fat".

C# add scheme to URI

I'm trying to add the HTTP Protocol to this URI "example:8888"
What I've done :
var uriBuilder = new UriBuilder("example:8888")
{
Scheme = Uri.UriSchemeHttp,
};
var uri = uriBuilder.Uri;
The Output is
http:0.0.34.184
What I'm doing wrong :S ?
Your string is being parsed as the URL path, not a hostname.
To force it to parse as a hostname, you need to add a scheme to the string.

UriBuilder and "../../" in uri

I need to combine two urls, but it seems that UriBuilder doesn't support url's with ../../ in them. Is my only option to code this by hand? I'm trying something like this :
Uri pageUri = new Uri("http://site.com/a/b/c.html");
string redirectUrl = "../../x.html";
UriBuilder builder = new UriBuilder(pageUri);
builder.Path += redirectUrl;
Thanks for any tips on how to do this the right way.
You could also to use:
Uri redirect = new Uri(
new Uri("http://site.com/a/b/c.html"), "../../x.html");
This is working fine for me. You tried to call builder.Uri.OriginalString to get the full address back?

Categories

Resources