Uri constructor with dontEscape is obsolete, what

2020-08-09 09:55发布

问题:

My question is regarding passing an URL to HttpWebRequest without escaping, I searched the forums and internet, but I didn't find a good solution for it.

I have following URL:string URL= www.website.com/sub/redirec\t\bs\dd

So when I create an uri like this:

Uri uri = new Uri(URL);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);

In this case on a get method I will get following URL:www.website.com/sub/redirect%5Ct%5Cbc%5Cdd

This sign "\" will be replaced by "%5C". What is crucial for me not to happen?

I can avoid that by:

Uri uri = new Uri(URL, true); //bool dontEscape

But this constructor is obsolete. How to have same effect without using obsolete?

回答1:

use this

Uri uri = new Uri(Uri.EscapeUriString(URL));