Initializes a new System.Net.WebRequest instance for the specified URI scheme.
- requestUri
A Uri containing the URI of the requested resource.
![]()
A System.Net.WebRequest descendant for the specified URI scheme.
Type Reason ArgumentNullException requestUri is null. NotSupportedException The request scheme specified in requestUri is not registered. System.Security.SecurityException The caller does not have permission to connect to the requested URI or a URI that the request is redirected to.
The WebRequest.CreateDefault(Uri) method returns a System.Net.WebRequest descendant instance based on only the scheme portion of a URI.
For example, when a URI beginning with http:// is passed in requestUri, an System.Net.HttpWebRequest is returned by WebRequest.CreateDefault(Uri). If a URI beginning with file:// is passed instead, the WebRequest.CreateDefault(Uri) method will return a System.Net.FileWebRequest.
This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].
Type | Reason |
---|---|
!:System.Security.Permissions.WebPermission | Requires permission to connect to the requested URI. See NetworkAccess.Connect. |
This example demonstrates the use of the WebRequest.Create(Uri, bool) and WebRequest.CreateDefault(Uri) methods.
C# Example
using System; using System.Net; public class ContosoTextRequest : WebRequest, IWebRequestCreate { public new WebRequest Create(Uri uri) { return new ContosoTextRequest(); } } public class CreateDefaultExample { public static void Main() { ContosoTextRequest contoso = new ContosoTextRequest(); Uri contosoUri = new Uri("http://www.contoso.com/text"); WebRequest.RegisterPrefix("http://www.contoso.com/text", contoso); WebRequest httpContoso = WebRequest.CreateDefault(contosoUri); Console.WriteLine("CreateDefault --> {0}", httpContoso); WebRequest textContoso = WebRequest.Create(contosoUri); Console.WriteLine("Create --> {0}", textContoso); } }
The output is
CreateDefault --> System.Net.HttpWebRequest