Serious Cat
Serious Cat

Reputation: 1

How call a custom url protocol in C#?

I try to call a custom protocol app from my API in C#.

my custom protocol is installed and I can call it in my browser with "my-app://myParams" URI, but I don't know how call a custom URL with webrequest. I tried to add a new object implementing IWebRequestCreate and called-it but I have stackoverflow error.

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 ?
    }
}

with the last code I have a stackoverflow exception on my WebRequest.Create(uri) method, but I don't know what to do in this method.

Thanks for your help

Upvotes: 0

Views: 1759

Answers (1)

shingo
shingo

Reputation: 27414

The docs says:

The Create method must return an initialized instance of the WebRequest descendant that is capable of performing a standard request/response transaction for the protocol without needing any protocol-specific fields modified.

So first you need to create a WebRequest descendant:

class AWebRequestDescendant : WebRequest
{
   ...
}

Then initialize and return it

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

Upvotes: 0

Related Questions