Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Net
WebRequest Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
WebRequest Class

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

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

Visual Basic (Declaration)
<SerializableAttribute> _
Public MustInherit Class WebRequest
    Inherits MarshalByRefObject
    Implements ISerializable
Visual Basic (Usage)
Dim instance As WebRequest
C#
[SerializableAttribute] 
public abstract class WebRequest : MarshalByRefObject, ISerializable
C++
[SerializableAttribute] 
public ref class WebRequest abstract : public MarshalByRefObject, ISerializable
J#
/** @attribute SerializableAttribute() */ 
public abstract class WebRequest extends MarshalByRefObject implements ISerializable
JScript
SerializableAttribute 
public abstract class WebRequest extends MarshalByRefObject implements ISerializable

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 WebException.Status property is one of the WebExceptionStatus values that indicates the source of the error. When WebException.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 System.Net.WebRequest.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.

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

Visual Basic
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
C#
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL.         
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
C++
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Text;
int main()
{
   
   // Create a request for the URL.   
   WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html" );
   
   // If required by the server, set the credentials.
   request->Credentials = CredentialCache::DefaultCredentials;
   
   // Get the response.
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
   
   // Display the status.
   Console::WriteLine( response->StatusDescription );
   
   // Get the stream containing content returned by the server.
   Stream^ dataStream = response->GetResponseStream();
   
   // Open the stream using a StreamReader for easy access.
   StreamReader^ reader = gcnew StreamReader( dataStream );
   
   // Read the content.
   String^ responseFromServer = reader->ReadToEnd();
   
   // Display the content.
   Console::WriteLine( responseFromServer );
   
   // Cleanup the streams and the response.
   reader->Close();
   dataStream->Close();
   response->Close();
}

  • WebPermission  To access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.
System.Object
   System.MarshalByRefObject
    System.Net.WebRequest
       System.Net.FileWebRequest
       System.Net.FtpWebRequest
       System.Net.HttpWebRequest
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Screen Scrape Images from a Web page      Dan Fernandez - MSFT   |   Edit   |   Show History

The following sample shows how to use the WebRequest class to screen scrape the contents of a Web page and use a regular expression to list the urls of the images on the page.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
class Program
{
    static void Main(string[] args)
    {
        //read images from a url and display them in the console window
        List<string> imageUrls = GetAllImagesFromUrl("http://www.msn.com");
               
        foreach (string img in imageUrls)
        {
            Console.WriteLine(img);
        }
        Console.Read();
    }
    /// <summary>
    /// Given a web page url, it will retrieve the Html from that page and parse the image tags in that page
    /// </summary>
    /// <param name="url">The Web page url in this format "http;//www.msn.com"</param>
    /// <returns>Returns a list of image urls as strings based on the url of a Web page</returns>
    public static List<string> GetAllImagesFromUrl(string url)
    {
        List<string> urlList = new List<string>();
        string rawHtml = String.Empty;
        //read the contents of the web page into a string
        using (StreamReader sr = new StreamReader(new WebClient().OpenRead(url)))
        {
            rawHtml = sr.ReadToEnd();
        }
        //regular expression to part out <img> tags from the html
        string regExPattern = @"< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )";
        Regex r = new Regex(regExPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
        MatchCollection matches = r.Matches(rawHtml);
       
        foreach (Match m in matches)
        {
            urlList.Add(m.Groups[1].Value);
        }
        return urlList;
    }

}
 

 

This sample is adopted from a Google images sample I wrote that shows how to asynchronously do WebRequests to screen scrape images from images.google.com. More information and full source code at: http://blogs.msdn.com/danielfe/archive/2004/07/26/197811.aspx 

Tags What's this?: Add a tag
Flag as ContentBug
Check file version      LucaMauri   |   Edit   |   Show History
Following a fragment of code that can help you determine the modification date of a file, so if you already have the last version, you can avaid downloading again
If File.Exists(destPath) Then
Try
imgRequest = WebRequest.Create(imgPath.TrimStart)
imgResp = imgRequest.GetResponse
lastMod = DateTime.Parse(imgResp.Headers("Last-Modified"))

imgInfo = New FileInfo(destPath)
If DateTime.Compare(imgInfo.CreationTime, lastMod) > 0 Then
Console.WriteLine("SKIP - " & destPath)
Continue For
End If
Catch ex As Exception
Console.WriteLine("ERROR - " & imgPath)
Finally
If imgResp IsNot Nothing Then
imgResp.Close()
End If
End Try
End If

Note this code does not include variables declaration or the For cycle that scan all the files. Also it refer to images, but of course it can be used with any kind of file.

Flag as ContentBug
Post data to a server      shiny zhu   |   Edit   |   Show History

We can use WebRequest perform a post.

Just need code in this way:

C#
string urlPost = "http://adventureworks.com/api/post.xml";
string postData = "status=something strange";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(postData);
 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPost);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;
request.Credentials = new NetworkCredential("xx", "xx");
 
Stream requestStream = request.GetRequestStream();
requestStream.Write(byteData, 0, byteData.Length);
requestStream.Close();

Maybe you think the request is working.

No, until you chek the response, the request is not performed.

C#
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseStatus = response.StatusDescription;
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker