HttpWebRequest setting error proxy(password error),but response success RRS feed

  • Question

  • My C# program calls Httpwebrequest to access the server and get a response. When my proxy is set incorrectly(password error), I hope response fails, but it responds successfully. Here is my setting.

    HttpWebRequest request = null;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "GET";
    request.UserAgent = userAgent;
    WebProxy proxy = new System.Net.WebProxy("http://" + ProxyServer + ":" + ProxyPort);
    request.Proxy = proxy;
    NetworkCredential ncProxy = new NetworkCredential(UserName, PassWord);
    request.Proxy.Credentials = ncProxy;
    request.Timeout = 15000;
    response = request.GetResponse() as HttpWebResponse;

    Tuesday, February 25, 2020 1:41 AM

All replies

  • The defacto is to use HTTPClient().
    Tuesday, February 25, 2020 7:27 AM
  • What you mean it responds succesfully? Do you mean you can get HttpWebResponse instance or do you mean HTTP response status is successful? Or what?

    If you mean you can get HttpWebResponse in general, then I would expect that not exception. What you need to do is check the response whether or not it was successful or not (StatusCode).

    Tuesday, February 25, 2020 9:55 AM
  • I configured ccproxy for testing.
    In the environment of ccproxy, there is an error(407) in response when configuring the wrong password.
    So I guess this phenomenon should be related to the setting of proxy server.
    Wednesday, February 26, 2020 1:32 AM
  • 407 response is expected in this case. So you can check that and handle it how appropriate for your code like asking user to check password or change configuration. Or if you are creating somekind library, then you can throw your own exception on that response.
    Wednesday, February 26, 2020 5:29 AM