プロキシサーバーを指定してダウンロードする個々のWebRequestで使用するプロキシを設定するWebRequest.ProxyプロパティにWebProxyオブジェクトを設定することにより、プロキシサーバーを指定できます。プロキシサーバーに「localhost:8080」を指定する例を以下に示します。 'HttpWebRequestオブジェクトの作成 Dim webreq As System.Net.HttpWebRequest = _ CType(System.Net.WebRequest.Create("http://www.yahoo.com"), _ System.Net.HttpWebRequest) 'プロキシの設定 'プロキシサーバーに"localhost:8080"を指定 Dim proxy As New System.Net.WebProxy("http://localhost:8080") webreq.Proxy = proxy 'HttpWebResponseの取得 Dim webres As System.Net.HttpWebResponse = _ CType(webreq.GetResponse, System.Net.HttpWebResponse) '受信して表示 Dim st As System.IO.Stream = webres.GetResponseStream() Dim sr As New System.IO.StreamReader(st) Console.WriteLine(sr.ReadToEnd()) '閉じる sr.Close() st.Close() //HttpWebRequestオブジェクトの作成 System.Net.HttpWebRequest webreq = (System.Net.HttpWebRequest) System.Net.WebRequest.Create("http://www.yahoo.com"); //プロキシの設定 //プロキシサーバーに"localhost:8080"を指定 System.Net.WebProxy proxy = new System.Net.WebProxy("http://localhost:8080"); webreq.Proxy = proxy; //HttpWebResponseの取得 System.Net.HttpWebResponse webres = (System.Net.HttpWebResponse) webreq.GetResponse(); //受信して表示 System.IO.Stream st = webres.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(st); Console.WriteLine(sr.ReadToEnd()); //閉じる sr.Close(); st.Close(); なお、.NET Framework 2.0からは、WebClientクラスにProxyプロパティが追加されました。これを使用して、WebClientで使用するプロキシを設定できます。 バイパスを設定するWebProxyコンストラクタの2番目のパラメータにTrueを指定すると、ローカルアドレスのプロキシをバイパスする(プロキシを通さずに、直接アクセスする)ようになります(または、WebProxy.BypassProxyOnLocalプロパティをTrueにします)。また、WebProxyコンストラクタの3番目のパラメータに、バイパスするURIの正規表現パターンを文字列の配列で指定できます(または、WebProxy.BypassListプロパティを設定します)。 'ローカルと"dobon.net"をバイパスする Dim bypassUrls() As String = {"dobon\.net"} webreq.Proxy = _ New System.Net.WebProxy("http://localhost:8080", True, bypassUrls) //ローカルと"dobon.net"をバイパスする string[] bypassUrls = new string[] {"dobon\\.net"}; webreq.Proxy = new System.Net.WebProxy("http://localhost:8080", true ,bypassUrls); Internet Exploreの設定を使用するInternet Exploreの設定を使用するには、WebProxy.GetDefaultProxyメソッドを使用します。
'IEの設定を使用する
webreq.Proxy = System.Net.WebProxy.GetDefaultProxy()
//IEの設定を使用する
webreq.Proxy = System.Net.WebProxy.GetDefaultProxy();
プロキシを使用しないようにする次のように、GlobalProxySelection.GetEmptyWebProxyプロパティを使用します。
'プロキシを使用しない
webreq.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy()
//プロキシを使用しない
webreq.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
プロキシ認証プロキシサーバーに認証が必要な場合は、WebProxyコンストラクタの4番目のパラメータにICredentialsを指定します。または、WebProxy.Credentialsプロパティを設定します。 Dim proxy As New System.Net.WebProxy("http://localhost:8080") 'ユーザー名とパスワードを設定 proxy.Credentials = New System.Net.NetworkCredential("name", "pass") webreq.Proxy = proxy
System.Net.WebProxy proxy =
new System.Net.WebProxy("http://localhost:8080");
//ユーザー名とパスワードを設定
proxy.Credentials = new System.Net.NetworkCredential("name", "pass");
webreq.Proxy = proxy;
すべてのWebRequestで使用するプロキシを設定するGlobalProxySelection.Selectメソッドにプロキシの設定を格納すると、Proxyの設定がされていないすべてのWebRequestで使用されるようになります。WebClientクラスでも内部ではWebRequestを使用していますので、このように設定したプロキシが使われるようになるでしょう Dim proxy As New System.Net.WebProxy("http://localhost:8080") System.Net.GlobalProxySelection.Select = proxy
System.Net.WebProxy proxy =
new System.Net.WebProxy("http://localhost:8080");
System.Net.GlobalProxySelection.Select = proxy;
アプリケーション構成ファイルにプロキシの設定を記述するアプリケーションやコンピュータ構成ファイルに、プロキシの設定を記述しておくことができます。ここに記述された設定は、先に紹介したGlobalProxySelectionクラスによって返されるプロキシの設定として使用されます。なお、アプリケーション構成ファイルに関しては、こちらをご覧ください。 以下のような記述をアプリケーション構成ファイルに書き込むことにより、デフォルトで、"http://localhost:8080"のプロキシを使用し、ローカルアドレスと、"dobon.net"をバイパスするようになります。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://localhost:8080"
bypassonlocal="True"
/>
<bypasslist>
<add address="dobon\.net" />
</bypasslist>
</defaultProxy>
</system.net>
</configuration>
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。
|
|
Copyright(C) DOBON!. All rights reserved.
|