C# auto detect proxy settings

后端 未结 8 711
遇见更好的自我
遇见更好的自我 2020-12-01 06:37

C# 2008 SP1

I am using the code to detect if a proxy has been set under \"Internet Options\". If there is a proxy then I will set this in my webclient.

So I

相关标签:
8条回答
  • 2020-12-01 07:25

    Try the following:

    public string GetMeMyInfo(string searchCriteria)
    {
        // Instatiate the web service and declare the necessary variables
        WsService.WsServiceBus oWsGetInfo = new WsService.WsServiceBus();
        // Configure the Web Service Proxy
        oWsGetInfo.Proxy = System.Net.WebProxy.GetDefaultProxy();
        oWsGetInfo.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        // Invoke the web service
        return oWsGetInfo.GetInfo4Me(searchCriteria);
    }
    

    That will get the default proxy setting and credentials before invoking your web service for example.

    0 讨论(0)
  • 2020-12-01 07:26

    This works for me

            var proxy = WebRequest.GetSystemWebProxy();
            Uri testUrl = new Uri("http://proxy.example.com");
            var proxyUrl = proxy.GetProxy(testUrl);
            if (proxyUrl != testUrl)
                //Use your proxy here
            else
                //We are not using a proxy
    
    0 讨论(0)
提交回复
热议问题