NSUrlSessionDataDelegate:大多数方法不会被调用

时间:2018-08-30 10:06:29

标签: iis xamarin.ios nsurlsession

昨天我想对我的NSUrlSessionDataDelegate实现DidReceiveData方法。我注意到从没调用过DidReceiveData方法,并且在尝试查找错误时,我注意到其他方法都没有被调用。我很确定其他方法之前也能奏效,我不知道是否更改了某些内容,或者可能是Xamarin。iOS更新是原因,但也许我在这里错了,但它们从未奏效。 我可以通过方法DidReceiveResponse,DidReceiveData和DidCompleteWithError确认此问题。

这是创建NSUrlSession并处理响应的代码(注意:我称IIS WebMethods以XML回答):

           public async Task<string> Invoke(string sUrl)
    {
        session = NSUrlSession.FromConfiguration(NSUrlSessionConfiguration.DefaultSessionConfiguration, 
                                                 (INSUrlSessionDelegate)new SessionDelegate((x)=> _fehlermeldung = x),  // Übergebe Methode damit der SessionDelegate die Fehlermeldung weiterreichen kann
                                                 null);
        request = CreateGETRequest(CreateNSUrl(sUrl));

        var data = session.CreateDataTaskAsync(request, out dataTask);
        dataTask.Resume();
        NSUrlSessionDataTaskRequest response = null;
        try
        {
            response = await data;
        }
        catch (NSErrorException ex)
        {
            throw new Exception(ex.Error.LocalizedDescription + ": " + _fehlermeldung);
        }
        var doc = new XmlDocument();
        doc.LoadXml(response.Data.ToString());
        XmlNode elem = doc.DocumentElement.FirstChild;

        return elem.InnerText;
    }

    private NSUrl CreateNSUrl(string url)
    {
        string converted = ((NSString)url).CreateStringByAddingPercentEscapes(NSStringEncoding.UTF8);
        var nsurl = NSUrl.FromString(converted);

        if (nsurl == null)
            throw new Exception("Fehlerhafte URL: Aus '" + url + "' konnte kein gültiges NSUrl Objekt erzeugt werden.");

        return nsurl;
    }

    private NSMutableUrlRequest CreateGETRequest(NSUrl nsurl)
    {
        return new NSMutableUrlRequest(nsurl) { HttpMethod = "GET" };
    }

这是我对会话委托的实现:

    private class SessionDelegate : NSUrlSessionDataDelegate,  INSUrlSessionDelegate
    {
        private readonly Action<string> setFehler;
        public SessionDelegate(Action<string> setFehler)
        {
            this.setFehler = setFehler;
        }

        public override void DidReceiveResponse(NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
        {
            var http_response = response as NSHttpUrlResponse;
            if (http_response == null)
            {
                setFehler($"Keine HTTP Url Antwort erhalten: {Environment.NewLine}'{response}'");
                completionHandler(NSUrlSessionResponseDisposition.Cancel);
                return;
            }

            var status_code = (int)http_response.StatusCode;
            if (status_code == 200) 
            {
                completionHandler(NSUrlSessionResponseDisposition.Allow);
            }
            else
            {
                setFehler($"Verbindung abgewiesen, HTTP Status: { status_code}, '{ http_response.ToString()}'");
                completionHandler(NSUrlSessionResponseDisposition.Cancel);
            }
        }

        public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
        {
            // THIS DOES NOT GET CALLED :-(
        }

        public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
        {
            if (error != null)
            {
                task.Cancel();
                var ex = new NSErrorException(error);
                throw (ex);
            }
        }

        public override void DidReceiveChallenge(NSUrlSession session, NSUrlSessionTask task, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
        {
            [...] // This is working fine for Client Cert Authentication or Basic Authentication
        }
    }

DidReceiveChallenge方法确实被调用并且正在工作。我出于可读性考虑将其删除。 所有其他方法都不会被调用。我想知道我在做什么错。您可以从本机iOS用户那里找到相同的问题,但我不知道如何使用Xamarin.iOS来实现他们的解决方案。

感谢您的帮助,谢谢。

0 个答案:

没有答案