次の方法で共有


HttpWebRequest.Proxy プロパティ

要求に対して使用するプロキシ情報を取得または設定します。

Overrides Public Property Proxy As IWebProxy
[C#]
public override IWebProxy Proxy {get; set;}
[C++]
public: __property IWebProxy* get_Proxy();public: __property void set_Proxy(IWebProxy*);
[JScript]
public override function get Proxy() : IWebProxy;public override function set Proxy(IWebProxy);

プロパティ値

要求に対するプロキシで使用する WebProxy インスタンス。既定値は GlobalProxySelection.Select を呼び出して設定します。

例外

例外の種類 条件
ArgumentNullException Proxy が null 参照 (Visual Basic では Nothing) に設定されています。
InvalidOperationException 要求が GetRequestStreamBeginGetRequestStreamGetResponse 、または BeginGetResponse の呼び出しにより既に開始されています。
SecurityException 要求された操作を実行するためのアクセス許可が呼び出し元にありません。

解説

Proxy プロパティは、インターネット リソースへの要求を処理するために使用する WebProxy インスタンスを識別します。プロキシが使用されないように指定するには、 GlobalProxySelection.GetEmptyWebProxy メソッドが返すプロキシ インスタンスに Proxy プロパティを設定します。

GetRequestStream メソッド、 BeginGetRequestStream メソッド、 GetResponse メソッド、または BeginGetResponse メソッドの呼び出しによって要求を開始した後に Proxy プロパティを変更すると、 InvalidOperationException がスローされます。

使用例

[Visual Basic, C#, C++] 要求に対して使用するプロキシ情報を、 Proxy メソッドを使って取得する例を次に示します。

 
' Create a new request to the mentioned URL.                
Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("https://www.microsoft.com"), HttpWebRequest)
Dim myProxy As New WebProxy()
' Obtain the 'Proxy' of the  Default browser.  
myProxy = CType(myWebRequest.Proxy, WebProxy)
' Print the Proxy Url to the console.
Console.WriteLine(ControlChars.Cr + "The actual default Proxy settings are {0}", myProxy.Address)
Try
    Console.WriteLine(ControlChars.Cr + "Please enter the new Proxy Address that is to be set ")
    Console.WriteLine("(Example:http://myproxy.com:port)")
    Dim proxyAddress As String
    proxyAddress = Console.ReadLine()
    If proxyAddress.Length = 0 Then
        myWebRequest.Proxy = myProxy
    Else
        Console.WriteLine(ControlChars.Cr + "Please enter the Credentials")
        Console.WriteLine("Username:")
        Dim username As String
        username = Console.ReadLine()
        Console.WriteLine(ControlChars.Cr + "Password:")
        Dim password As String
        password = Console.ReadLine()
        ' Create a new Uri object.
        Dim newUri As New Uri(proxyAddress)
        ' Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
        myProxy.Address = newUri
        ' Create a NetworkCredential object and associate it with the Proxy property of request object.
        myProxy.Credentials = New NetworkCredential(username, password)
        myWebRequest.Proxy = myProxy
    End If
    Console.WriteLine(ControlChars.Cr + "The Address of the  new Proxy settings are {0}", myProxy.Address)
    Dim myWebResponse As HttpWebResponse = CType(myWebRequest.GetResponse(), HttpWebResponse)

[C#] 
// Create a new request to the mentioned URL.                
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("https://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the  Default browser.  
myProxy=(WebProxy)myWebRequest.Proxy;
// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.Address);
try
{
    Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
    Console.WriteLine("(Example:http://myproxy.com:port)");
    string proxyAddress;
    proxyAddress =Console.ReadLine();
    if(proxyAddress.Length>0)
    
    {
        Console.WriteLine("\nPlease enter the Credentials ");
        Console.WriteLine("Username:");
        string username;
        username =Console.ReadLine();
        Console.WriteLine("\nPassword:");
        string password;
        password =Console.ReadLine();                    
        // Create a new Uri object.
        Uri newUri=new Uri(proxyAddress);
        // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
        myProxy.Address=newUri;
        // Create a NetworkCredential object and associate it with the Proxy property of request object.
        myProxy.Credentials=new NetworkCredential(username,password);
        myWebRequest.Proxy=myProxy;
    }
    Console.WriteLine("\nThe Address of the  new Proxy settings are {0}",myProxy.Address);
    HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();

[C++] 
// Create a new request to the mentioned URL.
HttpWebRequest* myWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"https://www.microsoft.com"));
WebProxy* myProxy = new WebProxy();
// Obtain the 'Proxy' of the  Default browser.
myProxy=dynamic_cast<WebProxy*>(myWebRequest->Proxy);
// Print the Proxy Url to the console.
Console::WriteLine(S"\nThe actual default Proxy settings are {0}", myProxy->Address);
try {
   Console::WriteLine(S"\nPlease enter the new Proxy Address that is to be set:");
   Console::WriteLine(S"(Example:http://myproxy.com:port)");
   String* proxyAddress;
   proxyAddress =Console::ReadLine();
   if (proxyAddress->Length>0) {
      Console::WriteLine(S"\nPlease enter the Credentials ");
      Console::WriteLine(S"Username:");
      String* username;
      username =Console::ReadLine();
      Console::WriteLine(S"\nPassword:");
      String* password;
      password =Console::ReadLine();
      // Create a new Uri object.
      Uri* newUri = new Uri(proxyAddress);
      // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
      myProxy->Address=newUri;
      // Create a NetworkCredential object and associate it with the Proxy property of request object.
      myProxy->Credentials = new NetworkCredential(username, password);
      myWebRequest->Proxy=myProxy;
   }
   Console::WriteLine(S"\nThe Address of the  new Proxy settings are {0}",
      myProxy->Address);
   HttpWebResponse* myWebResponse =
      dynamic_cast<HttpWebResponse*>(myWebRequest->GetResponse());

[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

.NET Framework セキュリティ:

  • WebPermission (Proxy プロパティを取得または設定するために必要なアクセス許可) Unrestricted (関連する列挙体)

参照

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