Question

I am working on a project that uses proxy to obtain websites' HTML code.

Now, the trouble I have is that I want to use Username-Password authentication and not IP-Auth when connecting to the proxy.

I have written a sample code and ran it with Snippy. It worked. Then I copied the same code into a Visual Studio .NET 4.5 Project and it failed with the error: An existing connection was forcibly closed by the remote host when trying to get the response.

Here is the code:

    WebProxy[] proxies = { new WebProxy("ip", port) };
    proxies[0].Credentials = new NetworkCredential { UserName = "username", Password = "password" };

    string url = "https://www.google.com/search?q=s&num=50";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Proxy = proxies[0];

    using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
    {
        using (StreamReader response_stream = new StreamReader(res.GetResponseStream()) 
        {
            string html = response_stream.ReadToEnd();
            Console.WriteLine(html);
        }
    }

I have tried different variations. When I switch to authorization by IP, add mine and comment out the NetworkCredentials assignment, the code works perfectly both in Snippy and Visual Studio.

But why does it fail when using NetworkCredentials?

Was it helpful?

Solution

OK, I found the culprit. It was my proxy provider.

Note for everybody ever running into problems when testing proxy connection with HttpWebRequest -- try another seller. It might save you a couple of minutes. Or hours.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top