0

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.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
RJV
  • 55
  • 11
  • Are you saying that when you provide invalid credentials it fails and when you provide valid credentials it works? If that's the case, why would you expect it to work if you provided invalid credentials? – Handbag Crab Nov 06 '18 at 11:22
  • @HandbagCrab Valid credential followed by invalid credential, I am getting the response. – RJV Nov 06 '18 at 11:35
  • That's because each time you connect you have to authenticate. Just because you're coming from the same IP doesn't mean you're the same user. If it didn't work like this then you could have a bad actor on your PC that was able to use the same API without authenticating once you'd first authenticated! – Handbag Crab Nov 06 '18 at 11:51
  • @HandbagCrab Exactly.. For Ex : First time : Valid User name and Password -> Got the response -> Expected ------------------------------------------------------------------------------------------------------------------- SecondTime Invalid User name or Password->Got the response -> This is not expected. I mean, second time there is no authenticating the user . – RJV Nov 07 '18 at 07:13

0 Answers0