Expand
WebRequest Class

Makes a request to a Uniform Resource Identifier (URI). This is an abstract class.

Namespace:  System.Net
Assembly:  System (in System.dll)
Syntax

'Declaration

<SerializableAttribute> _
Public MustInherit Class WebRequest _
	Inherits MarshalByRefObject _
	Implements ISerializable
Remarks

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

The WebRequest class throws a WebException when errors occur while accessing an Internet resource. The Status property is one of the WebExceptionStatus values that indicates the source of the error. When Status is WebExceptionStatus.ProtocolError, the Response property contains the WebResponse received from the Internet resource.

Because the WebRequest class is an abstract class, the actual behavior of WebRequest instances at run time is determined by the descendant class returned by Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

NoteNote

Use the Create method to initialize new WebRequest instances. Do not use the WebRequest constructor.

NoteNote

If the application that creates the WebRequest object runs with the credentials of a Normal user, the application will not be able to access certificates installed in the local machine store unless permission has been explicitly given to the user to do so.

Notes to Inheritors

When you inherit from WebRequest, you must override the following members: Method, RequestUri, Headers, ContentLength, ContentType, Credentials, PreAuthenticate, GetRequestStream, BeginGetRequestStream, EndGetRequestStream, GetResponse, BeginGetResponse, and EndGetResponse. In addition, you must provide an implementation of the IWebRequestCreate interface, which defines the Create method used when you call Create. You must register the class that implements the IWebRequestCreate interface, using the RegisterPrefix method or the configuration file.

Examples

The following example shows how to create a WebRequest instance and return the response.


Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
    Public Class WebRequestGetExample

        Public Shared Sub Main()
            ' Create a request for the URL. 		
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            Console.WriteLine(response.StatusDescription)
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Cleanup the streams and the response.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub 'Main
    End Class 'WebRequestGetExample
End Namespace


.NET Framework Security

  • WebPermission 

    To access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.Net.WebRequest
      System.IO.Packaging.PackWebRequest
      System.Net.FileWebRequest
      System.Net.FtpWebRequest
      System.Net.HttpWebRequest
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Community ContentAdd
WebRequest Class Errors

Using SilverlightApplication1 and in a Xaml page inside a Button click event, I get the following error:

Error 7 'System.Net.WebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.WebRequest' could be found (are you missing a using directive or an assembly reference?) C:\Users\marc\documents\visual studio 2010\Projects\SilverlightApplication1\SilverlightApplication1\MainPage.xaml.cs 298 65 SilverlightApplication1

Appears as though the method GetResponse is outdated, and the code is outdated.

Esther Fan, MSFT: Thank you for your feedback. For these kinds of questions, please try the following forums:
MSDN: http://forums.microsoft.com/msdn
This class does not work in .NET 4.0 as of early July 2010

I upgraded a downloader program from .NET 3.5, and everything worked fine except WebRequest, which simply doesn't work. I Googled, and there were dozens of people getting the same error with no one finding any solution. I had to revert my project for now. Hope it gets fixed soon.

Esther Fan, MSFT: Thank you for your feedback. For these kinds of questions, please try the following forums:
MSDN: http://forums.microsoft.com/msdn


Page view tracker