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?
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'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.
How do I use either webclient or httpwebrequest to do two things:
1)Say after downloading the resource as a string using:
var result = x.DownloadString("http://randomsite.com);
there's a relative url(also query string):
Click here to get your name and age
how do I click(follow) on that link using webclient? after initially loading the resource in result. i was able to use htmlagilitypack to isolate the href but I would now like to follow it in code.
2) If the httpwebrequest does not redirect but instead loads the same page with different parameters how would i use webclient to retrieve the new url that is generated?
i.e if i call
var result = x.DownloadString("http://randomsite.com);
but this actually calls
http://randomsite.com/q?site=default
I then want to retrieve the second url
Thanks in advance
You can construct the url from the link and the link that you just downloaded like this:
Uri baseUri = new Uri("http://randomsite.com");
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
Console.WriteLine(myUri.ToString()); // gives you http://randomsite.com/q?name=john&age=50
This also works if you base Url has url parameters.
As for the second question, i guess you meant that the request was redirected and you want that url instead? Then the easiest way to do so is to sub-class WebClient described here.
Uri baseUri = new Uri("http://randomsite.com");
using(var client=new WebClient())
{
var result = client.DownloadString(myUri);
//get href via HtmlAgilityPack...
Uri myUri = new Uri(baseUri, "/q?name=john&age=50");
result = client.DownloadString(myUri);
}
How do I stop the System.Uri class from unencoding an encoded URL passed into its constructor? Consider the following code:-
Uri uri = new Uri("http://foo.bar/foo%2FBar");
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", false);
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", true);
Console.WriteLine(uri.AbsoluteUri);
In each case the output is "http://foo.bar/foo/bar". How can I ensure that after instantiating a Uri instance, Uri.AbsoluteUri will return "http://foo.bar/foo%2FBar"?
The Uri class isn't used to generate an escaped URI, although you can use uri.OriginalString to retrieve the string used for initialization. You should probably be using UrlEncode if you need to reencode a URI safely.
Also, as per http://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx the dontEscape parameter in the Uri initializer has been deprecated and will always be false.
UPDATE:
Seems someone found a way (hack) to do this - GETting a URL with an url-encoded slash
Often I want to change just one part of a URI and get a new URI object back.
In my current dilemma, I want to append .nyud.net, to use the CoralCDN.
I have a fully qualified URI fullUri. How can I, in effect, do this:
fullUri.Host = fullUri.Host + ".nyud.net";
This needs to work for almost any URL, and the PORT of the request needs to be maintained.
Any help would be much appreciated.
You can use an UriBuilder to modify individual parts of an Uri:
Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");
UriBuilder builder = new UriBuilder(uri);
builder.Host += ".nyud.net";
Uri result = builder.Uri;
// result is "http://stackoverflow.com.nyud.net/questions/2163191/"