プロキシサーバーを使用してWebサイトに接続する必要があります。これは手動で行うことができます。たとえば、オンラインプロキシhttp://zend2.com
を使用してから、にアクセスできますwww.google.com
。ただし、これはプログラムで実行する必要があります。クラスを使用できることはわかってWebProxy
いますが、プロキシサーバーを使用できるようにコードを作成するにはどうすればよいですか?
誰でも例としてコードスニペットを教えてもらえますか?
ありがとう
zend2 の動作を理解すると、次のような URL を入力できます。
http://zend2.com/bro.php?u=http%3A%2F%2Fwww.google.com&b=12&f=noreferr
グーグルを閲覧するため。
IC#、次のような URL を作成します。
string targetUrl = "http://www.google.com";
string proxyUrlFormat = "http://zend2.com/bro.php?u={0}&b=12&f=norefer";
string actualUrl = string.Format(proxyUrlFormat, HttpUtility.UrlEncode(targetUrl));
// Do something with the proxy-ed url
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(actualUrl));
HttpWebResponse resp = req.GetResponse();
string content = null;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
content = sr.ReadToEnd();
}
Console.WriteLine(content);
WebProxyクラスを使用できます
MSDN コード
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
あなたの場合
WebProxy proxyObject = new WebProxy("http://zend2.com",true);
WebRequest req = WebRequest.Create("www.google.com");
req.Proxy = proxyObject;