Simple.oData.Client中的System.Net.Sockets.SocketException

时间:2015-04-08 22:52:30

标签: c# xamarin odata

我有以下代码来使用Simple.oData.Client获取oData。它有效。当我通过手机上网时;但是,当我通过代理保护的公司以太网连接时,它给我以下错误。 我怎么能处理它?<​​/ p>

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:发生了一个或多个错误---&gt;   System.AggregateException:发生了一个或多个错误---&gt;   Simple.OData.Client.WebRequestException:意外的WebException   遇到---&gt; System.Net.WebException:错误:ConnectFailure   (连接被拒绝)---&gt; System.Net.Sockets.SocketException:   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   在System.Net.WebConnection.Connect(System.Net.HttpWebRequest   请求)[0x001c2] in   ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.8.2.4/src/mono/mcs/class/System/System.Net/WebConnection.cs:213   ---内部异常堆栈跟踪结束---

2 个答案:

答案 0 :(得分:1)

以下是解决方案:

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");
       }

答案 1 :(得分:0)

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