Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing URLs in .NET
    text
    copied!<p>i'm looking for a .NET Framework class that can parse URLs.</p> <p>Some examples of URL's that require parsing:</p> <ul> <li><code>server:8088</code></li> <li><code>server:8088/func1</code></li> <li><code>server:8088/func1/SubFunc1</code></li> <li><code>http://server</code></li> <li><code>http://server/func1</code></li> <li><code>http://server/func/SubFunc1</code></li> <li><code>http://server:8088</code></li> <li><code>http://server:8088/func1</code></li> <li><code>http://server:8088/func1/SubFunc1</code></li> <li><code>magnet://server</code></li> <li><code>magnet://server/func1</code></li> <li><code>magnet://server/func/SubFunc1</code></li> <li><code>magnet://server:8088</code></li> <li><code>magnet://server:8088/func1</code></li> <li><code>magnet://server:8088/func1/SubFunc1</code></li> </ul> <p>The problem is that the <code>Uri</code> and <code>UriBuilder</code> classes do not handle the URLs correctly. For example, they are confused by:</p> <pre><code>stackoverflow.com:8088 </code></pre> <h1>Background on Urls</h1> <p>The format of a Url is:</p> <pre><code> foo://example.com:8042/over/there?name=ferret#nose \_/ \_________/ \__/\_________/\__________/ \__/ | | | | | | scheme host port path query fragment </code></pre> <p>In our case, we only care about:</p> <ul> <li><code>Uri.Scheme</code></li> <li><code>Uri.Host</code></li> <li><code>Uri.Port</code></li> <li><code>Uri.Path</code></li> </ul> <h1>Tests</h1> <p>Running some tests, we can check how <code>UriBuilder</code> class handles various Uri's:</p> <pre><code> Expected Expected Expected Expected //Test URI Scheme Server Port Path //===================================== ======== ======== ==== ==================== t("server", "", "server", -1, ""); t("server/func1", "", "server", -1, "/func1"); t("server/func1/SubFunc1", "", "server", -1, "/func1/SubFunc1"); t("server:8088", "", "server", 8088, ""); t("server:8088/func1", "", "server", 8088, "/func1"); t("server:8088/func1/SubFunc1", "", "server", 8088, "/func1/SubFunc1"); t("http://server", "http", "server", -1, "/func1"); t("http://server/func1", "http", "server", -1, "/func1"); t("http://server/func/SubFunc1", "http", "server", -1, "/func1/SubFunc1"); t("http://server:8088", "http", "server", 8088, ""); t("http://server:8088/func1", "http", "server", 8088, "/func1"); t("http://server:8088/func1/SubFunc1", "http", "server", 8088, "/func1/SubFunc1"); t("magnet://server", "magnet", "server", -1, ""); t("magnet://server/func1", "magnet", "server", -1, "/func1"); t("magnet://server/func/SubFunc1", "magnet", "server", -1, "/func/SubFunc1"); t("magnet://server:8088", "magnet", "server", 8088, ""); t("magnet://server:8088/func1", "magnet", "server", 8088, "/func1"); t("magnet://server:8088/func1/SubFunc1", "magnet", "server", 8088, "/func1/SubFunc1"); </code></pre> <p>All but six cases fail to parse correctly:</p> <pre><code>Url Scheme Host Port Path =================================== ====== ====== ==== =============== server http server 80 / server/func1 http server 80 /func1 server/func1/SubFunc1 http server 80 /func1/SubFunc1 server:8088 server -1 8088 server:8088/func1 server -1 8088/func1 server:8088/func1/SubFunc1 server -1 8088/func1/SubFunc1 http://server http server 80 / http://server/func1 http server 80 /func1 http://server/func/SubFunc1 http server 80 /func1/SubFunc1 http://server:8088 http server 8088 / http://server:8088/func1 http server 8088 /func1 http://server:8088/func1/SubFunc1 http server 8088 /func1/SubFunc1 magnet://server magnet server -1 / magnet://server/func1 magnet server -1 /func1 magnet://server/func/SubFunc1 magnet server -1 /func/SubFunc1 magnet://server:8088 magnet server 8088 / magnet://server:8088/func1 magnet server 8088 /func1 magnet://server:8088/func1/SubFunc1 magnet server 8088 /func1/SubFunc1 </code></pre> <p>i said i wanted a .NET Framework class. i would also accept any code-gum laying around that i can pick up and chew. As long as it satisfies my simplistic test cases.</p> <h1>Bonus Chatter</h1> <p>i was looking at expanding <a href="https://stackoverflow.com/questions/14685147/how-can-i-parse-http-urls-in-c">this question</a>, but that question is limited to <code>http</code> only.</p> <p>i also asked this <a href="https://stackoverflow.com/questions/20164298/net-how-to-build-a-url">same question earlier today</a>, but i realize now that i phrased it incorrectly. i incorrectly asked how to <em>"build"</em> a url. When in reality i want to <em>"parse"</em> a user-entered URL. i can't go back and fundamentally change the title now. So i'll ask the same question again, only better, with more clearly stated goals, here.</p> <h1>Bonus Reading</h1> <ul> <li><a href="https://stackoverflow.com/questions/14685147/how-can-i-parse-http-urls-in-c">How can I parse HTTP urls in C#?</a></li> <li><a href="https://stackoverflow.com/questions/20164298/net-how-to-build-a-url">How to build a Url?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload