System.Net.Sockets.SocketException 在 Simple.oData.Client

System.Net.Sockets.SocketException in Simple.oData.Client

我有以下代码使用 Simple.oData.Client 获取 oData。它有效。当我通过手机连接 wifi 时 phone;但是,当我通过受代理保护的公司以太网连接时,出现以下错误。 我该如何处理?

async partial  void Click_TouchUpInside (UIButton sender)
        {
            var client= new ODataClient("http://services.odata.org/Northwind/Northwind.svc/");

            Console.WriteLine("before await");

            try {
            var packages = await client
                .For("Customers").
                FindEntriesAsync();
                foreach (var package in packages)
                 {
                  //Console.WriteLine(package["CompanyName"]);
                  Console.WriteLine(package);
                 }
                }
                catch(AggregateException e) {
                   Console.WriteLine(e);
                   Console.WriteLine(e.InnerException);
                }
                Console.WriteLine("after await");
       }

System.AggregateException: One or more errors occurred ---> System.AggregateException: One or more errors occurred ---> Simple.OData.Client.WebRequestException: Unexpected WebException encountered ---> System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000f1] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.8.2.4/src/mono/mcs/class/System/System.Net.Sockets/Socket_2_1.cs:1257 at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x001c2] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.8.2.4/src/mono/mcs/class/System/System.Net/WebConnection.cs:213 --- End of inner exception stack trace ---

这意味着您尝试连接的端点没有侦听连接。

解决方法如下:

async partial  void Click_TouchUpInside (UIButton sender)
        {

            ODataClientSettings settings = new ODataClientSettings();

            NetworkCredential proxyNC = new NetworkCredential("Username", "password");

            settings.OnApplyClientHandler= (System.Net.Http.HttpClientHandler clientHandler) => {

                clientHandler.Proxy = new WebProxy("yourproxy.com",false,null, proxyNC);
                clientHandler.UseProxy = true;
            };
            settings.UrlBase = "http://services.odata.org/Northwind/Northwind.svc/";
            var client= new ODataClient(settings);

            Console.WriteLine("before await");

            try {
            var packages = await client
                .For("Customers").
                FindEntriesAsync();
                foreach (var package in packages)
                 {
                  //Console.WriteLine(package["CompanyName"]);
                  Console.WriteLine(package["CompanyName"]);
                 }
                }
                catch(AggregateException e) {
                   Console.WriteLine(e);
                   Console.WriteLine(e.InnerException);
                }
                Console.WriteLine("after await");
       }