改变的System.Uri的方案 [英] Changing the scheme of System.Uri

查看:163
本文介绍了改变的System.Uri的方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找改变一个给定的的System.Uri 实例与 System.UriBuilder 没有蹩脚的字符串操作和魔术常量。说我有

I'm looking for canonical way of changing scheme of a given System.Uri instance with System.UriBuilder without crappy string manipulations and magic constants. Say I have

var uri = new Uri("http://localhost/hello")

和我需要将其更改为 HTTPS 。我的问题是有限的 UriBuilder 构建函数和 Uri.Port 默认为80(我们应该将其更改为443?硬编码? )。在code必须尊重所有乌里属性,如可能的基本身份验证凭据,查询字符串,等等。

and I need to change it to 'https'. My issue is in limited UriBuilder ctors and Uri.Port defaulting to 80 (should we change it to 443? hardcoding?). The code must respect all Uri properties such as possible basic auth credentials, query string, etc.

推荐答案

结束了这一个:

var uriBuilder = new UriBuilder(requestUrl)
{
    Scheme = Uri.UriSchemeHttps,
    Port = -1 // default port for scheme
};

这篇关于改变的System.Uri的方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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