HttpWebRequest URL Output

I know the name sounds like this issue has been examined many times. But I am struggling with a specific case, and I am very confused about this. Hopefully experienced C # 'er could point me in the right direction.

I have a code:

string serviceURL = "https://www.domain.com/service/tables/bucketname%2Ftables%2Ftesttable/imports";
HttpWebRequest dataRequest = (HttpWebRequest)WebRequest.Create(serviceURL);

Now when I read dataRequest, I see the following:

RequestUri: {https://www.domain.com/service/tables/bucketname/tables/testtable/imports}

And it looks like HttpWebRequest has changed %2Fto /. However, the server requires that the requested Uri be exactly the same as the serviceURL containing is written %2F.

Is there a way to get the HttpWebRequest class to call Url:

https://www.domain.com/service/tables/bucketname%2Ftables%2Ftesttable/imports

Many thanks! Here I have a complete loss ... -Brett

+3
source share
3 answers

, , : URL- URL-

, , , .

+3

, % 2F "/", . .Net, app.config. : System.Uri unescape% 2f (slash) ?

, , '(' ')' (% 28 % 29). , , , Uri WebRequest. % 2F, % 28 % 29, , .

, WebRequest 1 Uri "GET/path HTTP/1.1": Uri.PathAndQuery, UriParser.GetComponents.

% 28 % 29, , .Net % 28 % 29 '(' ')' (: " " ).

, , .

, ( .Net 4.6) PathAndQuery, UriParser .

public sealed class MyUriParser : System.UriParser
{
    private UriParser _originalParser;
    private MethodInfo _getComponentsMethod;

    public MyUriParser(UriParser originalParser) : base()
    {
        if (_originalParser == null)
        {
            _originalParser = originalParser;

            _getComponentsMethod = typeof(UriParser).GetMethod("GetComponents", BindingFlags.NonPublic | BindingFlags.Instance);
            if (_getComponentsMethod == null)
            {
                throw new MissingMethodException("UriParser", "GetComponents");
            }
        }
    }

    private static Regex rx = new Regex(@"^(?<Scheme>[^:]+):(?://((?<User>[^@/]+)@)?(?<Host>[^@:/?#]+)(:(?<Port>\d+))?)?(?<Path>([^?#]*)?)?(\?(?<Query>[^#]*))?(#(?<Fragment>.*))?$",RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline);
    private Match m = null;

    protected override string GetComponents(Uri uri, UriComponents components, UriFormat format)
    {
        var original = (string)_getComponentsMethod.Invoke(_originalParser, BindingFlags.InvokeMethod, null, new object[] { uri, components, format }, null);
        if (components == UriComponents.PathAndQuery)
        {
            var reg = rx.Match(uri.OriginalString);
            var path = reg.Groups["Path"]?.Value;
            var query = reg.Groups["Query"]?.Value;
            if (path != null && query != null) return $"{path}?{query}";
            if (query == null) return $"{path}";
            return $"{path}";
        }

        return original;
    }

}

Uri, UriParser .

    public static Uri CreateUri(string url)
    {
        var uri = new Uri(url);
        if (url.Contains("%28") || url.Contains("%29"))
        {
            var originalParser = ReflectionHelper.GetValueByReflection(uri, "m_Syntax") as UriParser;
            var parser = new MyUriParser(originalParser);
            ReflectionHelper.SetValueByReflection(parser, "m_Scheme", "http");
            ReflectionHelper.SetValueByReflection(parser, "m_Port", 80);
            ReflectionHelper.SetValueByReflection(uri, "m_Syntax", parser);
        }
        return uri;
    }

, UriParser, , 2 , . "http", . ReflectionHelper - , , .

:

HttpWebRequest dataRequest = (HttpWebRequest)WebRequest.Create(CreateUri(serviceURL));
0
string serviceURL = Uri.EscapeUriString("https://www.domain.com/service/tables/bucketname%2Ftables%2Ftesttable/imports");
-1
source

Source: https://habr.com/ru/post/1779018/