How to Set a Proxy for Webbrowser Control Without Effecting the System/Ie Proxy

C# WebBrowser Control Proxy

private Uri currentUri;

private void Form1_Load(object sender, EventArgs e)
{
currentUri = new Uri(@"http://www.stackoverflow.com");
HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.stackoverflow.com");
//WebProxy myProxy = new WebProxy("208.52.92.160:80");
//myRequest.Proxy = myProxy;

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

webBrowser1.DocumentStream = myResponse.GetResponseStream();

webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}

void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.AbsolutePath != "blank")
{
currentUri = new Uri(currentUri, e.Url.AbsolutePath);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri);

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

webBrowser1.DocumentStream = myResponse.GetResponseStream();
e.Cancel = true;
}
}

You'll have to play with it a little, but I was able to browse around the site.

Or you can try modifying the WebRequest.DefaultWebProxy setting:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx

Set a proxy in C#?

By default, your C# code will use the system proxy set in your IE connection settings.

If you want to be explicit, you can do it via config under System.net:

<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="true" proxyaddress="[proxy address]" bypassonlocal="true" />
</defaultProxy>

Programmatically, you can set the proxy:

http://msdn.microsoft.com/en-us/library/system.net.webproxy.aspx

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

EDIT:
For web browser control, try this SO post: (disclaimer - haven't tried that)

How to set a proxy for Webbrowser Control without effecting the SYSTEM/IE proxy

Set Proxy Credential in Web Browser Control

I actually found a work'able solution, where it was lie under navigation with Proxy-Authentication in header:

var credentialStringValue = "user:pass";
var credentialByteArray = ASCIIEncoding.ASCII.GetBytes(credentialStringValue);
var credentialBase64String = Convert.ToBase64String(credentialByteArray);

Object nullObject = 0;
Object nullObjectString = "";
Object authObject = string.Format("Proxy-Authorization: Basic {0}{1}", credentialBase64String, Environment.NewLine);

browser.Navigate(args.Url, ref nullObject, ref nullObject, ref nullObjectString, ref authObject);

where browser is:

public AxWebBrowser browser;

Specify Proxy for WebBrowser control in WP7, WP8

The WebBrowser control is tied to the OS, therefore inheriting the proxy settings that are definied by the user of the device. You can't directly modify them, unless you want to intercept navigation events and feed them directly to your server.



Related Topics



Leave a reply



Submit