次の方法で共有


HttpWebRequest.Proxy プロパティ

定義

要求のプロキシ情報を取得または設定します。

public:
 virtual property System::Net::IWebProxy ^ Proxy { System::Net::IWebProxy ^ get(); void set(System::Net::IWebProxy ^ value); };
public override System.Net.IWebProxy? Proxy { get; set; }
public override System.Net.IWebProxy Proxy { get; set; }
member this.Proxy : System.Net.IWebProxy with get, set
Public Overrides Property Proxy As IWebProxy

プロパティ値

要求のプロキシに使用する IWebProxy オブジェクト。 既定値は、Select プロパティを呼び出すことによって設定されます。

例外

Proxynullに設定されます。

呼び出し元には、要求された操作に対するアクセス許可がありません。

次のコード例では、Proxy メソッドを使用して要求のプロキシ情報を取得します。

// Create a new request to the mentioned URL.
HttpWebRequest ^ myWebRequest =
    (HttpWebRequest ^) (WebRequest::Create("http://www.microsoft.com"));

// Obtain the 'Proxy' of the  Default browser.  
IWebProxy ^ proxy = myWebRequest->Proxy;
// Print the Proxy Url to the console.
if (proxy) 
{
    Console::WriteLine("Proxy: {0}",
        proxy->GetProxy(myWebRequest->RequestUri));
} 
else 
{
    Console::WriteLine("Proxy is null; no proxy will be used");
}

WebProxy ^ myProxy = gcnew WebProxy;

Console::WriteLine("\nPlease enter the new Proxy Address that is to be set:");
Console::WriteLine("(Example:http://myproxy.example.com:port)");
String ^ proxyAddress;

try 
{
    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 = gcnew 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 =
            gcnew NetworkCredential(username, password);
        myWebRequest->Proxy = myProxy;
    }
    Console::WriteLine("\nThe Address of the  new Proxy settings are {0}",
                  myProxy->Address);
    HttpWebResponse ^ myWebResponse =
        (HttpWebResponse ^) (myWebRequest->GetResponse());
// Create a new request to the mentioned URL.				
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");

// Obtain the 'Proxy' of the  Default browser.
IWebProxy proxy = myWebRequest.Proxy;
// Print the Proxy Url to the console.
if (proxy != null)
{
    Console.WriteLine("Proxy: {0}", proxy.GetProxy(myWebRequest.RequestUri));
}
else
{
    Console.WriteLine("Proxy is null; no proxy will be used");
}

WebProxy myProxy=new WebProxy();

Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
Console.WriteLine("(Example:http://myproxy.example.com:port)");
string proxyAddress;

try
{
    proxyAddress =Console.ReadLine();
    if(proxyAddress.Length>0)
    {
        Console.WriteLine("\nPlease enter the Credentials (may not be needed)");
        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();
' Create a new request to the mentioned URL.				
Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.microsoft.com"), HttpWebRequest)

      ' Obtain the 'Proxy' of the  Default browser.  
      Dim proxy as IWebProxy = CType(myWebRequest.Proxy, IWebProxy)
      ' Print the Proxy Url to the console.
If Not proxy Is Nothing Then
    Console.WriteLine("Proxy: {0}", proxy.GetProxy(myWebRequest.RequestUri))
Else
    Console.WriteLine("Proxy is null; no proxy will be used")
End If

Dim myProxy As New WebProxy()

Console.WriteLine(ControlChars.Cr + "Please enter the new Proxy Address that is to be set ")
Console.WriteLine("(Example:http://myproxy.example.com:port)")
Dim proxyAddress As String
Try
    proxyAddress = Console.ReadLine()
    If proxyAddress.Length = 0 Then
        myWebRequest.Proxy = myProxy
    Else
        Console.WriteLine(ControlChars.Cr + "Please enter the Credentials (may not be needed)")
        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)

注釈

注意

WebRequestHttpWebRequestServicePointWebClient は廃止されており、新しい開発には使用しないでください。 代わりに HttpClient を使用してください。

Proxy プロパティは、インターネット リソースへの要求の処理に使用する WebProxy オブジェクトを識別します。 プロキシを使用しない場合は、Proxy プロパティを、GlobalProxySelection.GetEmptyWebProxy メソッドによって返されるプロキシ インスタンスに設定します。

ローカル コンピューターまたはアプリケーション構成ファイルでは、既定のプロキシを使用することを指定できます。 Proxy プロパティが指定されている場合、Proxy プロパティのプロキシ設定はローカル コンピューターまたはアプリケーション構成ファイルをオーバーライドし、HttpWebRequest インスタンスは指定されたプロキシ設定を使用します。 構成ファイルにプロキシが指定されておらず、Proxy プロパティが指定されていない場合、HttpWebRequest クラスはローカル コンピューター上のインターネット オプションから継承されたプロキシ設定を使用します。 インターネット オプションにプロキシ設定がない場合、要求はサーバーに直接送信されます。

HttpWebRequest クラスは、ローカル プロキシ バイパスをサポートしています。 次のいずれかの条件が満たされた場合、クラスは宛先をローカルと見なします。

  • 宛先には、フラット名が含まれています (URL にドットはありません)。

  • 宛先にループバック アドレス (Loopback または IPv6Loopback) が含まれているか、または宛先にローカル コンピューターに割り当てられた IPAddress が含まれています。

  • 宛先のドメイン サフィックスは、ローカル コンピューターのドメイン サフィックス (DomainName) と一致します。

GetRequestStreamBeginGetRequestStreamGetResponse、または BeginGetResponse メソッドを呼び出して、要求の開始後に Proxy プロパティを変更すると、InvalidOperationExceptionがスローされます。 プロキシ要素の詳細については、< を参照してください。defaultProxy>要素 (ネットワーク設定)

適用対象

こちらもご覧ください