Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a Url?

Tags:

c#

.net

Are there any helper classes available in .NET to allow me to build a Url?

For example, if a user enters a string:

stackoverflow.com 

and i try to pass that to an HttpWebRequest:

WebRequest.CreateHttp(url); 

It will fail, because it is not a valid url (it has no prefix).

What i want is to be able to parse the partial url the user entered:

Uri uri = new Uri(url); 

and then fix the missing pieces:

if (uri.Port == 0)    uri.Port = 3333; if (uri.Scheme == "")    uri.Scheme = "https"; 

Does .NET have any classes that can be used to parse and manipulate Uri's?

The UriBuilder class can't do the job

The value that the user entered (e.g. stackoverflow.com:3333) is valid; i just need a class to pick it apart. i tried using the UriBuilder class:

UriBuilder uriBuilder = new UriBuilder("stackoverflow.com:3333"); 

unfortunately, the UriBuilder class is unable to handle URIs:

  • uriBuilder.Path = 3333
  • uriBuilder.Port = -1
  • uriBuidler.Scheme = stackoverflow.com

So i need a class that can understand host:port, which especially becomes important when it's not particularly http, but could be.

Bonus Chatter

Console application.

From the other question

Some examples of URL's that require parsing:

  • server:8088
  • server:8088/func1
  • server:8088/func1/SubFunc1
  • http://server
  • http://server/func1
  • http://server/func/SubFunc1
  • http://server:8088
  • http://server:8088/func1
  • http://server:8088/func1/SubFunc1
  • magnet://server
  • magnet://server/func1
  • magnet://server/func/SubFunc1
  • magnet://server:8088
  • magnet://server:8088/func1
  • magnet://server:8088/func1/SubFunc1
  • http://[2001:db8::1]
  • http://[2001:db8::1]:80

The format of a Url is:

  foo://example.com:8042/over/there?name=ferret#nose   \_/   \_________/ \__/\_________/\__________/ \__/    |         |        |     |           |        | scheme      host    port   path       query   fragment 

Bonus Chatter

Just to point out again that UriBuilder does not work:

  • https://dotnetfiddle.net/s66kdZ
like image 619
Ian Boyd Avatar asked Nov 23 '13 15:11

Ian Boyd


People also ask

How do you create a URL?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

What is URL in JavaScript?

URL() The URL() constructor returns a newly created URL object representing the URL defined by the parameters. If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown. Note: This feature is available in Web Workers.

What is a URL object?

The vanilla JavaScript URL object is used to parse, construct, normalize, and encode URLs. It provides static methods and properties to easily read and modify different components of the URL.

What is URL in API?

The URL API is a component of the URL standard, which defines what constitutes a valid Uniform Resource Locator and the API that accesses and manipulates URLs.


2 Answers

If you need to ensure that some string coming as user input is valid url you could use the Uri.TryCreate method:

Uri uri; string someUrl = ... if (!Uri.TryCreate(someUrl, UriKind.Absolute, out uri)) {     // the someUrl string did not contain a valid url      // inform your users about that } else {     var request = WebRequest.Create(uri);     // ... safely proceed with executing the request } 

Now if on the other hand you want to be building urls in .NET there's the UriBuilder class specifically designed for that purpose. Let's take an example. Suppose you wanted to build the following url: http://example.com/path?foo=bar&baz=bazinga#some_fragment where the bar and bazinga values are coming from the user:

string foo = ... coming from user input string baz = ... coming from user input  var uriBuilder = new UriBuilder("http://example.com/path"); var parameters = HttpUtility.ParseQueryString(string.Empty); parameters["foo"] = foo; parameters["baz"] = baz; uriBuilder.Query = parameters.ToString(); uriBuilder.Fragment = "some_fragment";  Uri finalUrl = uriBuilder.Uri; var request = WebRequest.Create(finalUrl); ... safely proceed with executing the request 
like image 57
Darin Dimitrov Avatar answered Sep 26 '22 17:09

Darin Dimitrov


You can use the UriBuilder class.

var builder = new UriBuilder(url); builder.Port = 3333 builder.Scheme = "https";  var result = builder.Uri; 
like image 38
Lee Avatar answered Sep 22 '22 17:09

Lee