【问题标题】:How call a custom url protocol in C#?如何在 C# 中调用自定义 url 协议?
【发布时间】:2019-12-20 17:30:41
【问题描述】:

我尝试从我的 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 异常,但我不知道在这个方法中要做什么。

感谢您的帮助

【问题讨论】:

  • I have a stackoverflow exception 请向我们展示堆栈。
  • Webrequest.Create 里面没有调用前缀创建者吗?我希望当您注册您的前缀时,这意味着您的处理程序将被调用,并且在您的处理程序中您调用 Webrequest.Create 来调用您的处理程序等。
  • referencesource.microsoft.com/#system/net/system/Net/… 从手机上看不太清楚,但是我认为有在 Create 方法中查找前缀处理程序的逻辑。
  • 你做过这个工作吗?

标签: c# custom-protocol


【解决方案1】:

docs 说:

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

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

class AWebRequestDescendant : WebRequest
{
   ...
}

然后初始化并返回

WebRequest IWebRequestCreate.Create(Uri uri)
{
    return new AWebRequestDescendant();
}

【讨论】:

    猜你喜欢
    • 2013-12-12
    • 2010-09-09
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多