如何在 C# 中解析 HTTP url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14685147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:40:16  来源:igfitidea点击:

How can I parse HTTP urls in C#?

c#parsingurl

提问by vijay053

My requirement is to parse Http Urls and call functions accordingly. In my current implementation, I am using nested if-else statement which i think is not an optimized way. Can you suggest some other efficient approch?

我的要求是解析 Http Urls 并相应地调用函数。在我当前的实现中,我使用嵌套的 if-else 语句,我认为这不是优化的方式。你能建议一些其他有效的方法吗?

Urls are like these:

网址是这样的:

  • server/func1
  • server/func1/SubFunc1
  • server/func1/SubFunc2
  • server/func2/SubFunc1
  • server/func2/SubFunc2
  • server/func1
  • server/func1/SubFunc1
  • server/func1/SubFunc2
  • server/func2/SubFunc1
  • server/func2/SubFunc2

采纳答案by Suncat2000

I think you can get a lot of use out of the System.Uriclass. Feed it a URI and you can pull out pieces in a number of arrangements.

我认为您可以充分利用System.Uri类。为它提供一个 URI,您可以通过多种方式提取碎片。

Some examples:

一些例子:

Uri myUri = new Uri("http://server:8080/func2/SubFunc2?query=somevalue");

// Get host part (host name or address and port). Returns "server:8080".
string hostpart = myUri.Authority;

// Get path and query string parts. Returns "/func2/SubFunc2?query=somevalue".
string pathpart = myUri.PathAndQuery;

// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;

// Get query string. Returns "?query=somevalue".
string querystring = myUri.Query;

回答by Tim

I combined the split in Suncat2000's answer with string splitting to get at interesting features of the URL. I am passing in a full Uri including https: etc. from another page as the navigation argument e.Parameter:

我将 Suncat2000 的答案中的拆分与字符串拆分相结合,以获得 URL 的有趣功能。我从另一个页面传入一个完整的 Uri,包括 https: 等作为导航参数 e.Parameter:

Uri playlistUri = (Uri)e.Parameter;
string youtubePlaylistUnParsed = playlistUri.Query;
char delimiterChar = '=';
string[] sections = youtubePlaylistUnParsed.Split(delimiterChar);
string YoutubePlaylist = sections[1];

This gets me the playlist in the PLs__ etc. form for use in the Google APIs.

这让我获得了 PLs__ 等形式的播放列表,以便在 Google API 中使用。

回答by Luiso

This might come as a bit of a late answer but I found myself recently trying to parse some URLs and I went along using a combination of Uriand System.Web.HttpUtilityas seen here, my URLs were like http://one-domain.com/some/segments/{param1}?param2=x....so this is what I did:

这可能会作为一个有点晚了答案,但我发现自己最近试图解析一些网址和我一起使用的组合就UriSystem.Web.HttpUtility所看到这里,我的网址都喜欢http://one-domain.com/some/segments/{param1}?param2=x....,所以这是我做的事:

var uri = new Uri(myUrl);
string param1 = uri.Segments.Last();

var parameters = HttpUtility.ParseQueryString(uri.Query);
string param2 = parameters["param2"];

note that in both cases you'll be working with strings, and be specially weary when working with segments.

请注意,在这两种情况下,您都将使用strings,并且在使用段时会特别疲倦。