我该如何解析HTTP网址,在C#中? [英] How can I parse HTTP urls in C#?

查看:123
本文介绍了我该如何解析HTTP网址,在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是要分析的HTTP URL,并相应地调用函数。在我目前的实施中,我使用嵌套的if-else语句,我认为是不是一个优化的方式。你可以建议一些其他有效的计算策略

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?

URL是这样的:

Urls are like these:


  • 服务器/ FUNC1

  • 服务器/ FUNC1 / SubFunc1

  • 服务器/ FUNC1 / SubFunc2

  • 服务器/ FUNC2 / SubFunc1

  • 服务器/ FUNC2 / SubFunc2

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

推荐答案

我觉得你可以得到大量的使用了的的System.Uri 类。 。给它一个URI,你可以在一些安排拔出片

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

一些例子:

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;

这篇关于我该如何解析HTTP网址,在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆