Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to connect to the internet through a proxy, more specificlly, this site to test if the proxy is working AZ Environment variables 1.04 As you can see down below.. It is NOT connecting using the proxy I set it to, and as you can see in the code below, it should be.. I used the debugger, everything was fine, it was flowing through the code the way it should.

Why is the
C#
webBrowser1.Navigate(browserNavigationTxtBox.Text);
not using the proxy address I set it too in
C#
proxyBox.Text


Image showing whats going on.
http://i.stack.imgur.com/ek2Lq.png[^]



C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Net;
using Fiddler;
using Fiddler.WebFormats;

namespace Switch_IP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Uri currentUri;

        private void Form1_Load(object sender, EventArgs e)
        {
            currentUri = new Uri(browserNavigationTxtBox.Text);
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(browserNavigationTxtBox.Text);
            var myProxy = new WebProxy(proxyBox.Text);
            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 != browserNavigationTxtBox.Text)
            {
                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;
            }
        }

        private void Start_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(browserNavigationTxtBox.Text);

        }
    }
}


What I have tried:

Basically all I've tried is whats in the code, it looked alot different in the beginning, the code was a big mess
Posted
Updated 22-May-16 13:40pm

1 solution

You are not setting your Proxy server in the webBrowser1_Navigating event handler, only in your Form_Load event handler
Add the code to the webBrowser1_Navigating event handler to make your HttpWebRequest use the Proxy
C#
WebProxy myProxy = new WebProxy(proxyBox.Text);
myRequest.Proxy = myProxy;

In Form_Load you have created a HttpWebRequest & set the Proxy
In the WebBrowser Navigating event handler you have created another HttpWebRequest but have not set the proxy server..

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900