如何使用C#WebClient访问wss端点

问题描述 投票:0回答:1

我试图用json-rpc调用WebClient端点。它有前缀wss,我无法做到这一点。

我曾尝试使用WebRequest.RegisterPrefix来修复它,但我尝试创建一个IWebRequestCreate只会导致“此属性不是由此类实现的”。

        static void Send(string methodName, string parametersJObject, int id)
        {

            JObject bodyJObject =
                new JObject(
                    new JProperty("jsonrpc", "2.0"),
                    new JProperty("method", methodName),
                    new JProperty("id", id.ToString()),
                    new JProperty("params", parametersJObject)
                    );


            var cli = new WebClient();

            cli.Headers[HttpRequestHeader.ContentType] = "application/json";
            string postBody = bodyJObject.ToString(Formatting.None);
            string response = cli.UploadString("wss://somesite.com/ws", postBody);

        }

我试图添加这个(我发现它在SO上)

        public class FakeRequest : WebRequest
        {
            private Uri _uri;

            public FakeRequest(Uri uri)
            {
                _uri = uri;
            }

            public override Uri RequestUri { get { return _uri; } }
        }

        public class FakeRequestFactory : IWebRequestCreate
        {
            public WebRequest Create(Uri uri)
            {
                return new FakeRequest(uri);
            }
        }

并使用注册

       WebRequest.RegisterPrefix("wss://", new FakeRequestFactory());

我认为正确实施的IWebRequestCreate是关键,但我已经豁免了所有选项,并想知道你是否有答案。

它是用.NET Framework 4.7编写的,但是如果更好的话我可以切换到.NET Core。

提前致谢

/ Soeren

c# webclient prefix wss json-rpc
1个回答
0
投票

我尝试了WebSockets而且工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.