Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 UserAgent Property

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

Other versions are also available for the following:
.NET Framework Class Library
HttpWebRequest..::.UserAgent Property

Gets or sets the value of the User-agent HTTP header.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Property UserAgent As String
Visual Basic (Usage)
Dim instance As HttpWebRequest
Dim value As String

value = instance.UserAgent

instance.UserAgent = value
C#
public string UserAgent { get; set; }
Visual C++
public:
property String^ UserAgent {
    String^ get ();
    void set (String^ value);
}
JScript
public function get UserAgent () : String
public function set UserAgent (value : String)

Property Value

Type: System..::.String
The value of the User-agent HTTP header. The default value is nullNothingnullptra null reference (Nothing in Visual Basic).
NoteNote:

The value for this property is stored in WebHeaderCollection. If WebHeaderCollection is set, the property value is lost.

The following code example sets the UserAgent property.

Visual Basic
           ' Create a new 'HttpWebRequest' object to the mentioned URL.
            Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
            myHttpWebRequest.UserAgent= ".NET Framework Test Client"
            ' The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
            Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
            ' Display the contents of the page to the console.
            Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
            Dim streamRead As New StreamReader(streamResponse)
            Dim readBuff(256) As [Char]
            Dim count As Integer = streamRead.Read(readBuff, 0, 256)
            Console.WriteLine(ControlChars.Cr + "The contents of HTML Page are :" + ControlChars.Cr)
            While count > 0
                Dim outputData As New [String](readBuff, 0, count)
                Console.Write(outputData)
                count = streamRead.Read(readBuff, 0, 256)
            End While
            streamRead.Close()
             streamResponse.Close()    
          ' Release the response object resources.
             myHttpWebResponse.Close()

C#
            // Create a new 'HttpWebRequest' object to the mentioned URL.
            HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
            myHttpWebRequest.UserAgent=".NET Framework Test Client";
            // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
            HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
            // Display the contents of the page to the console.
            Stream streamResponse=myHttpWebResponse.GetResponseStream();
            StreamReader streamRead = new StreamReader( streamResponse );
            Char[] readBuff = new Char[256];
            int count = streamRead.Read( readBuff, 0, 256 );
            Console.WriteLine("\nThe contents of HTML Page are :\n");    
            while (count > 0) 
            {
                String outputData = new String(readBuff, 0, count);
                Console.Write(outputData);
                count = streamRead.Read(readBuff, 0, 256);
            }
            // Release the response object resources.
            streamRead.Close();
            streamResponse.Close();
            myHttpWebResponse.Close();

Visual C++
// Create a new 'HttpWebRequest' object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.contoso.com" ) );
myHttpWebRequest->UserAgent = ".NET Framework Test Client";
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
// Display the contents of the page to the console.
Stream^ streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader^ streamRead = gcnew StreamReader( streamResponse );
array<Char>^ readBuff = gcnew array<Char>(256);
int count = streamRead->Read( readBuff, 0, 256 );
Console::WriteLine( "\nThe contents of HTML Page are :\n" );
while ( count > 0 )
{
   String^ outputData = gcnew String( readBuff,0,count );
   Console::Write( outputData );
   count = streamRead->Read( readBuff, 0, 256 );
}
streamRead->Close();
streamResponse->Close();
myHttpWebResponse->Close();

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

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker