second time proxy validation failing with webRequest API

42 Views Asked by At

in Windows application, webRequest.GetResponse() Behavior :

1) First time, I tried with "invalid user name/ credentials" and getting the below error.

"ERROR: The remote server returned an error: (407) Proxy Authentication Required."

and now I am giving valid "user name and credentials" -> getting the response

2) Now the reverse scenario, i.e I called the API with valid userName and password -> working

after this, if I try with an invalid credential, I am getting the response.

Code Snippet:

private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = string.Empty;
        var webProxy = new WebProxy("http://proxy:80/",true)
        {
            Credentials = new NetworkCredential("UserName", "PassWord"),
            UseDefaultCredentials = false
        };


        WebRequest.DefaultWebProxy = webProxy;
        try
        {
            var webRequest = (HttpWebRequest)WebRequest.Create("http://Google.co.in");
            webRequest.Proxy = webProxy;
            webRequest.Timeout = 30 * 1000;
            using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                label1.Text = string.Format("WebRequest Response Code : {0}. Web Request Status : {1}", webResponse.StatusCode, webResponse.StatusDescription);
            }

        }
        catch (Exception ex)
        {
            label1.Text = ex.Message;
        }
        finally { WebRequest.DefaultWebProxy = null; }

    }

I am not able to find the exact reason, how the call become success, please help me to understand the above scenario.

0

There are 0 best solutions below