Export (0) Print
Expand All

HttpWebRequest.UserAgent Property

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

[Visual Basic]
Public Property UserAgent As String
[C#]
public string UserAgent {get; set;}
[C++]
public: __property String* get_UserAgent();
public: __property void set_UserAgent(String*);
[JScript]
public function get UserAgent() : String;
public function set UserAgent(String);

Property Value

The value of the User-agent HTTP header. The default value is a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following 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();

[C++] 
// Create a new 'HttpWebRequest' object to the mentioned URL.
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com"));
myHttpWebRequest->UserAgent = S".NET Framework Test Client";
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<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(S"\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();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft