[英]How to access wss endpoint using C# WebClient
我試圖使用WebClient
調用json-rpc
端點。 它有前綴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。
提前致謝
/索倫
我嘗試了WebSockets而且工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.