次の方法で共有


HttpWebRequest.UserAgent プロパティ

User-agent HTTP ヘッダーの値を取得または設定します。

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);

プロパティ値

User-agent HTTP ヘッダーの値。既定値は null 参照 (Visual Basic では Nothing) です。

使用例

[Visual Basic, C#, C++] UserAgent プロパティを設定する例を次に示します。

 
' Create a new 'HttpWebRequest' object to the mentioned URL.
 Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://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("https://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"https://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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

HttpWebRequest クラス | HttpWebRequest メンバ | System.Net 名前空間