1

I am in a company network. In this network i can't ping external websites by IP. I can only call them by url like the browser does. Thats why i use WebRequest to see if Internetconnection is established.

When i try to call "www.google.com" i got a "(407) Proxyauthentification required"

This Programm should be used on many pcs. So i dont want to set a Credential User and Password hardcoded in the Code.

This is my Code:

try
        {
            var uriBuilder = new UriBuilder(_URL);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
            request.Timeout = 1000;
            //request.Accept = "*/*";
            WebProxy proxy = new WebProxy("14*.***.***.***:8080");
            //request.UseDefaultCredentials = true; //Dont work
            //proxy.Credentials = System.Net.NetworkCredential(); //Dont work
            proxy.Credentials = CredentialCache.DefaultNetworkCredentials; //dont work too
            request.Proxy = proxy;
            request.PreAuthenticate = true;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            _PingByURL = response.StatusCode == HttpStatusCode.OK;

        }
        catch 
        {

            _PingByURL =false;
        }

And my app.cfg:

<system.net>
  <defaultProxy enabled ="true" useDefaultCredentials = "true">
     <proxy usesystemdefault ="True" bypassonlocal="True"/> <!--True must written in capital Letters-->
  </defaultProxy>
</system.net>

How can i solve error "(407) Proxyauthentification required"? Or is there a better way to check if Internetconnection is established through proxy without pinging it?

Shadow
  • 31
  • 1
  • 5
  • Are you *sure* your company's proxy doesn't require a specific username/password? `DefaultCredentials` means use the current user's Windows Account (not username/password but the actual account). – Panagiotis Kanavos Nov 09 '15 at 08:33
  • it requires the domain user and password. He doesn't have to type it. He uses it automaticly. – Shadow Nov 09 '15 at 08:34
  • So the `DefaultCredentials` aren't the Credentials if you logged in as a Domain User? – Shadow Nov 09 '15 at 08:37
  • You confuse credentials with accounts. In Windows, credentials are entered only when a user logs in. After that, it's the *account* that is used to authorize, not the username/password. If the proxy recognizes Windows authentication, it should work. – Panagiotis Kanavos Nov 09 '15 at 08:42
  • Actually, you shouldn't have to specify *anything*. The application should use the system's proxy settings by default. Have you tried to make a request without modifying the proxy settings? There is a [similar, possibly duplicate question](http://stackoverflow.com/questions/299940/how-should-i-set-the-default-proxy-to-use-default-credentials) about this – Panagiotis Kanavos Nov 09 '15 at 08:42
  • Yes. If i delete the Credentials and PreAuthentification it still doesn't work. Getting (407). – Shadow Nov 09 '15 at 08:51
  • But if i delete Proxy completly he still dosen't use it. So he didn't use the System proxy settings by default. – Shadow Nov 09 '15 at 08:55

1 Answers1

0

I found my mistake.

  <system.net>
    <defaultProxy enabled ="true" useDefaultCredentials = "true">
      <!--<proxy usesystemdefault ="True" bypassonlocal="True"/>-->  //This is the fault
    </defaultProxy>
  </system.net>

This line causes the trouble. I never thougt about this in my app config because of the solutions in other threads about this line.

But now it only works in Visual Studio. If i try the .exe without Visual Studio it still says "407 Authenticationrequired"

Shadow
  • 31
  • 1
  • 5
  • Remove the following two lines and give it another go, request.Proxy = proxy; request.PreAuthenticate = true; – Don Nov 10 '15 at 04:29
  • Yes it worked. but now it only worked if i run it on Visual Studio. If i run the *.exe alone it doesn't work any more. But why? – Shadow Nov 10 '15 at 06:24
  • Ah. i need the (application).exe.config with the exe to let it work. – Shadow Nov 10 '15 at 06:39