0

我尝试从 C# 中的 API 调用自定义协议应用程序。

我的自定义协议已安装,我可以在浏览器中使用“my-app://myParams”URI 调用它,但我不知道如何使用 webrequest 调用自定义 URL。我试图添加一个实现 IWebRequestCreate 并调用它的新对象,但我有 stackoverflow 错误。

WebRequest.RegisterPrefix("my-app", new MyCustomWebRequestCreator());
WebRequest req = WebRequest.Create("my-app:");

internal class CustomWebRequestCreator : IWebRequestCreate
{
    WebRequest IWebRequestCreate.Create(Uri uri)
    {
        return WebRequest.Create(uri); // what can I do here ?
    }
}

使用最后一个代码,我的 WebRequest.Create(uri) 方法上有一个 stackoverflow 异常,但我不知道在这个方法中要做什么。

谢谢你的帮助

4

1 回答 1

0

文档说:

Create 方法必须返回 WebRequest 后代的初始化实例,该实例能够为协议执行标准请求/响应事务,而无需修改任何特定于协议的字段。

所以首先你需要创建一个 WebRequest 后代:

class AWebRequestDescendant : WebRequest
{
   ...
}

然后初始化并返回

WebRequest IWebRequestCreate.Create(Uri uri)
{
    return new AWebRequestDescendant();
}
于 2019-08-14T08:36:24.520 回答