NSURLSession GET请求:DidCompleteWithError - 错误为空

时间:2018-04-16 09:21:11

标签: xamarin xamarin.ios nsurlsession

目前我正在尝试从已弃用的NSUrlConnection切换到NSUrlSession。 我尝试实现的是通过GET请求调用IIS Webservice,然后处理XML答案。 我有两个问题:

  1. 如果答案太长,DidReceiveData中的NSData对象不包含所有内容。答案是在1036个字符后裁剪的。
  2. 完成所有操作后,当我已经处理了接收到的数据时(无论是否被裁剪),都会调用DidCompleteWithError。它中的“error”参数为null,因此我不知道为什么调用它以及如何处理它。所以,即使我收到DidReceiveData中的所有数据,一切都很好,也会调用DidCompleteWithError。
  3. 这是我的代码,感谢每一位帮助。提前谢谢!

    public class WebserviceCallerIOS : IWebServiceCaller
        {
            private NSMutableUrlRequest request;
            public NSUrlSessionDataTask dataTask;
            private Action<bool, string> completed;
            private string antwort;
            private NSUrlSession session;
            public NSOperationQueue myQueue;
    
            public void Invoke(string sUrl)
            {
                session = null;
                antwort = null;
                completed = null;
                myQueue = new NSOperationQueue();
    
                session = NSUrlSession.FromConfiguration(NSUrlSessionConfiguration.DefaultSessionConfiguration, (INSUrlSessionDelegate)new SessionDelegate((erfolg, body) => { completed(erfolg, body); }), myQueue);
                completed += WebserviceCallerIOS_Completed;
    
                try
                {
                    request = CreateNativePostRequest(sUrl);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    completed(false, "Error creating request: " + e);
                }
    
                dataTask = session.CreateDataTask(request);
                dataTask.Resume();
            }
    
            NSMutableUrlRequest CreateNativePostRequest(string url)
            {
                string converted = ((NSString)url).CreateStringByAddingPercentEscapes(NSStringEncoding.UTF8);
                var nsurl = NSUrl.FromString(converted);
    
                if (nsurl == null)
                    throw new Exception("Invalid URL, could not create NSUrl from: '" + url + "'.");
    
                var req = new NSMutableUrlRequest(nsurl) {HttpMethod = "GET"};
                return req;
            }
    
    private class SessionDelegate : NSUrlSessionDataDelegate, INSUrlSessionDelegate
            {
                private Action<bool, string> completed_callback;
                private NSData retData;
                private int status_code;
    
                public SessionDelegate(Action<bool, string> completed)
                {
                    completed_callback = completed;
                    retData = new NSData();
                }
    
    
                public override void DidReceiveResponse(NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
                {
                    var http_response = response as NSHttpUrlResponse;
                    if (http_response == null)
                    {
                        Console.WriteLine("Received non HTTP url response: '{0}'", response);
                        status_code = -1;
                        return;
                    }
    
    
                    status_code = (int)http_response.StatusCode;
    
                    if (status_code == 200)
                {
                    completionHandler(NSUrlSessionResponseDisposition.Allow);
                }
                else 
                {
                    completionHandler(NSUrlSessionResponseDisposition.Cancel);
                }
            }
    
    
                public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
                {
                    completed_callback(false, error?.LocalizedDescription);
                }
    
    
                public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
                {
                    retData = data;
                    dataTask.Suspend();
                    Console.WriteLine(retData);
                    completed_callback(true, retData.ToString());
                }
        }
    }
    

1 个答案:

答案 0 :(得分:1)

  

如果答案太长,DidReceiveData中的NSData对象不包含所有内容。答案在1036个字符后被裁剪。

DidReceiveData将被触发并多次返回小数据。您必须创建全局变量来存储数据。但是你的意思是数据一次超过1036个字符?

  

完成所有操作后,当我已经处理了接收到的数据时(无论是否被裁剪),都会调用DidCompleteWithError。它中的“error”参数为null,因此我不知道为什么调用它以及如何处理它。所以,即使我收到DidReceiveData中的所有数据,一切都很好,也会调用DidCompleteWithError。

请求是否成功,在收到所有数据后始终会调用DidCompleteWithError。错误null表示您的请求成功。