Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to website using a free proxy server programmatically

I need to connect to a website using a proxy server. I can do this manually, for example I can use the online proxy http://zend2.com and then surf to www.google.com. But this must be done programmatically. I know I can use WebProxy class but how can I write a code so a proxy server can be used?

Anyone can give me a code snippet as example or something?

thanks

like image 568
Ozkan Avatar asked May 25 '12 11:05

Ozkan


2 Answers

Understanding of zend2 works, you can populate an url like this :

http://zend2.com/bro.php?u=http%3A%2F%2Fwww.google.com&b=12&f=norefer

for browsing google.

I C#, build the url like this :

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);
like image 150
Steve B Avatar answered Oct 31 '22 12:10

Steve B


You can use WebProxy Class

MSDN code

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;

In your case

WebProxy proxyObject = new WebProxy("http://zend2.com",true);
WebRequest req = WebRequest.Create("www.google.com");
req.Proxy = proxyObject;
like image 37
ABH Avatar answered Oct 31 '22 12:10

ABH