System.Net.WebRequest.RegisterPrefix Method

Registers a System.Net.WebRequest descendant for the specified URI.

Syntax

public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)

Parameters

prefix
The complete URI or URI prefix that the System.Net.WebRequest descendant services.
creator
The create method that the System.Net.WebRequest calls to create the System.Net.WebRequest descendant.

Returns

true if registration is successful; otherwise, false.

Exceptions

TypeReason
ArgumentNullException prefix is null or creator is null.

Remarks

The WebRequest.RegisterPrefix(string, IWebRequestCreate) method registers System.Net.WebRequest descendants to service requests. System.Net.WebRequest descendants are typically registered to handle a specific protocol, such HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The pre-registered reserve types already registered include the following:

  • http://

  • https://

  • ftp://

  • file://

For more information, see the WebRequest.Create(string) and WebRequest.Create(Uri) methods.

Duplicate prefixes are not allowed. WebRequest.RegisterPrefix(string, IWebRequestCreate) returns false if an attempt is made to register a duplicate prefix.

Note:

The System.Net.HttpWebRequest class is registered to service requests for HTTP and HTTPS schemes by default. Attempts to register a different System.Net.WebRequest descendant for these schemes will fail.

Example

The following example demonstrates how to register a new scheme.

C# Example

using System;
using System.Net;

public class ftpWebRequest : WebRequest {
   //implement ftp-specific protocol methods and properties
}

public class ftpCreator : IWebRequestCreate 
{
   public WebRequest Create(Uri uri) 
   {
      return new ftpWebRequest(); 
   }
}

public class RegisterPrefixExample
{

   public static void Main() 
   {
 
      ftpCreator creator = new ftpCreator();
      WebRequest.RegisterPrefix("ftp://", creator);
      WebRequest wr = WebRequest.Create("ftp://testFile");
      Console.WriteLine(wr);
   }
}
   

The output is

ftpWebRequest

Requirements

Namespace: System.Net
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0