合并URI和路径 [英] Combining URIs and Paths

查看:43
本文介绍了合并URI和路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对应用程序进行改造,以使用PHP HTTP代理(用于缓存),而不是实际的API服务器,该应用程序当前将服务器URI和路径与代码结合在一起:

I am retro-fitting an application to make use of a PHP HTTP proxy (for caching) instead of the actual API server, the application currently combines the server URI and the path with the code:

methodUri = new Uri(apiUri, method.Path)

位置:

  • apiUri = "http://api.eve-online.com/" (System.Uri Object)
  • method.Path = "/char/SkillIntraining.xml.aspx" (string)

上述声明的结果是

"http://api.eve-online.com/char/SkillIntraining.xml.aspx" (System.Uri Object)

要使用PHP HTTP代理,必须按如下所述更改请求

To use the PHP HTTP proxy the request would have to be changed as follows

  • apiUri = "http://www.r-s.co.uk/eproxy.php" (System.Uri Object)
  • method.Path = "/char/SkillIntraining.xml.aspx" (string)

我期望的输出是:

"http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx" (System.Uri Object)

但是我得到的输出是:

"http://www.r-s.co.uk/char/SkillIntraining.xml.aspx" (System.Uri Object)

我知道这是构造函数Uri(Uri,string)的正确功能,我的问题是,用哪个更好的函数或构造函数代替它以获得我期望的输出?我尝试删除method.Path中的前导"/",但是从绝对路径到相对路径却没有帮助.

I understand that this is the correct functionality of the constructor Uri(Uri, string), my question is what would be a better function or constructor to use in its place to get the output I expect? I have tried removing the leading "/" in method.Path taking it from an absolute path to a relative path however that did not help.

注意::以下两种解决方案都可以使用,但是System.UriBuilder提供了一种更强大的机制来组合URI和路径,在我的情况下,与使用System.Uri相比,对资源的更改更少.如果我选择的话,我会将两个答案都标记为正确.

NOTE: both solutions below do work, however System.UriBuilder provides a more robust mechanism for combining URI's and paths and in my case resulted in fewer changes to resources than using System.Uri. Had I the choice I would mark both answers as correct.

推荐答案

不要使用Uri对象,请使用

Don't use the Uri object, use a UriBuilder - it copes way better with missing slashes

所以

Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php");
string methodPath = "/char/SkillIntraining.xml.aspx";

System.UriBuilder uriBuilder = new System.UriBuilder(apiUri);
uriBuilder.Path += methodPath;

Console.WriteLine(uriBuilder.Uri.ToString());

按预期工作,并产生 http://www.rs.co.uk/eproxy.php/char/SkillIntraining.xml.aspx

这篇关于合并URI和路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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