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.
Related
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");
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.
*EDIT: This doesn't happen on Windows but on Mono 4.2.2 Linux (C# Online Compiler).
I want to parse the protocol-relative URL and get the host name etc. For now I insert "http:" to the head before processing it since C# Uri class couldn't handle a protocol-relative URL. Could you tell me if there's any better way or any good library?
// Protocol-relative URL
var uriString = "//www.example.com/bluh/bluh.css";
var uri = new Uri(uriString);
Console.WriteLine(uriString); // "//www.example.com/bluh/bluh.css"
Console.WriteLine(uri.Host); // "Empty" string
// Absolute URL
var fixUriString = uriString.StartsWith("//") ? "http:" + uriString : uriString;
var fixUri = new Uri(fixUriString);
Console.WriteLine(fixUriString); // "http://www.example.com/bluh/bluh.css"
Console.WriteLine(fixUri.Host); // "www.example.com"
This works:
Uri uri = null;
if(Uri.TryCreate("//forum.xda-developers.com/pixel-c", UriKind.Absolute, out uri))
{
Console.WriteLine(uri.Authority);
Console.WriteLine(uri.Host);
}
returns
forum.xda-developers.com
forum.xda-developers.com
It also worked for me using the Uri(string) constructor.
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);
I have some strings like this:
www.example.com/sdWqaP
twitter.com/sdfks
and want to assign them to a HyperLink
var hyperlink = new Hyperlink
{
NavigateUri = new Uri(url),
TargetName = "_blank",
};
if url starts with http:// it works fine, otherwise throws a UriFormatException.
Update: urls like this www.google.com aren't valid http urls. isn't there a better way than var url = "http://" + "www.google.com"
You can use
var uri = new UriBuilder(s).Uri;
Reference: http://msdn.microsoft.com/en-us/library/y868d5wh(v=vs.110).aspx
public UriBuilder(
string uri
)
// If uri does not specify a scheme, the scheme defaults to "http:".
Scheme (http:// in your case) is mandatory part of Uri string. UriFormatException will be thrown if the scheme specified in uri string is not correctly formed according to Uri.CheckSchemeName() method.
[MSDN : Uri Constructor (String)].
I don't understand well what you mean "better safer way". Appending scheme in uri string is common practice anyway.
Check your URL is valid and then assign to the URL
For validating a URL check the below link
How to check whether a string is a valid HTTP URL?