C# 解析URL的优雅方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15713542/
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
Elegant way parsing URL
提问by Bill Gates
After POST/GET request I get such URL back which I need to parse, of course I can go and use spit() to get required information, but for sure should be more elegant way of doing that. Any ideas?
在 POST/GET 请求之后,我得到了我需要解析的 URL,当然我可以去使用 spit() 来获取所需的信息,但肯定应该是更优雅的方式。有任何想法吗?
http://api.vkontakte.ru/blank.html#access_token=8860213c0a392ba0971fb35bdfb0z605d459a9dcf9d2208ab60e714c3367681c6d091aa12a3fdd31a4872&expires_in=86400&user_id=34558123
http://api.vkontakte.ru/blank.html#access_token=8860213c0a392ba0971fb35bdfb0z605d459a9dcf9d2208ab60e714c3367681c6d091aa12a3fdd31a4872&expires_in=86400&user_id=34558123
I am parsing for: access token
and expires_in
我正在解析:access token
和expires_in
回答by Jeroen Bouman
Use Uri + ParseQueryString functions:
使用 Uri + ParseQueryString 函数:
Uri myUri = new Uri("http://api.vkontakte.ru/blank.html#access_token=8860213c0a392ba0971fb35bdfb0z605d459a9dcf9d2208ab60e714c3367681c6d091aa12a3fdd31a4872&expires_in=86400&user_id=34558123");
String access_token = HttpUtility.ParseQueryString(myUri.Query).Get("access_token");
String expires_in = HttpUtility.ParseQueryString(myUri.Query).Get("expires_in");
This will also do the trick
这也能解决问题
String access_token = HttpUtility.ParseQueryString(myUri.Query).Get(0);
Source: https://msdn.microsoft.com/en-us/library/ms150046.aspx
来源:https: //msdn.microsoft.com/en-us/library/ms150046.aspx
Tip: You might need
提示:您可能需要
using System.Web;
And add a reference to System.Web
并添加对 System.Web 的引用
回答by Blairg23
There are several ways you can do this. One is that you can simply use the Uri.Query
method to get the query string and then parse by the &s. Another is that you can use the Uri.Query
method and then use HttpUtility.ParseQueryString
to parse the query string as a NameValueCollection
, which might be your preferred route.
有几种方法可以做到这一点。一种是你可以简单地使用该Uri.Query
方法来获取查询字符串,然后通过 &s 进行解析。另一个是您可以使用该Uri.Query
方法,然后使用该方法HttpUtility.ParseQueryString
将查询字符串解析为 a NameValueCollection
,这可能是您的首选路线。
See the example below:
请参阅下面的示例:
using System.Web; // For HttpUtility
// The original URL:
Uri unparsedUrl = new Uri("http://api.vkontakte.ru/blank.html#access_token=8860213c0a392ba0971fb35bdfb0z605d459a9dcf9d2208ab60e714c3367681c6d091aa12a3fdd31a4872&expires_in=86400&user_id=34558123");
// Grabs the query string from the URL:
string query = unparsedUrl.Query;
// Parses the query string as a NameValueCollection:
var queryParams = HttpUtility.ParseQueryString(query);
You can now perform operations similar to how you would deal with a Dictionary
object. Like so:
您现在可以执行类似于处理Dictionary
对象的操作。像这样:
string accessToken = queryParams["access_token"];
string expiresIn = queryParams["expires_in"];
This has the same functionality as what @Jeroen Bouman showed, but splits apart the different functions so you can understand what each piece does individually.
这与@Jeroen Bouman 显示的功能相同,但将不同的功能分开,以便您可以了解每个部分的作用。
References:
参考: