Пример #1
0
        private void ExecSendRequest(ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            float timeout = Time.realtimeSinceStartup + Timeout;

            this.SetSDKClientInfo();
            WWWRequestLooper.RegisterNetworkRequest(new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary()), progressCallback, (WWW www, ProgressCallbackHelper progress) => {
                if (!www.isDone)
                {
                    if (timeout < Time.realtimeSinceStartup)
                    {
                        DisposeWWW(www);
                        callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                        return(true);
                    }
                    if (progress != null)
                    {
                        progress.NotifyUploadProgress(www, this.body.Length);
                    }
                    return(false);
                }
                else
                {
                    try
                    {
                        Exception e = this.CheckHttpError(www);
                        if (e != null)
                        {
                            callback(null, e);
                            return(true);
                        }
                        ApiResponse response = new ApiResponse();
                        Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
                        this.CopyHttpHeaders(responseHeaders, response);
                        response.Status      = WWWUtils.GetStatusCode(responseHeaders, www.bytes == null ? 204 : 200);
                        response.ContentType = WWWUtils.GetHeader(responseHeaders, "Content-Type");
                        response.ETag        = WWWUtils.GetHeader(responseHeaders, "ETag");
                        if (www.bytes != null)
                        {
                            response.Body = this.GetString(www.bytes);
                        }
                        callback(response, null);
                        return(true);
                    }
                    catch (Exception e)
                    {
                        if (Kii.Logger != null)
                        {
                            Kii.Logger.Debug("[ERROR] Unexpected exception occurred when handling http response. msg=" + e.Message);
                        }
                        callback(null, e);
                        return(true);
                    }
                    finally
                    {
                        DisposeWWW(www);
                    }
                }
            });
        }
 /// <summary>
 /// Registers the network request.
 /// </summary>
 /// <param name="www">Www.</param>
 /// <param name="progressCallback">Callback for progrees.</param>
 /// <param name="action">Action.</param>
 internal static void RegisterNetworkRequest(WWW www, ProgressCallbackHelper progressCallback, Func <WWW, ProgressCallbackHelper, bool> action)
 {
     WWWRequestLooper.RunOnMainThread(() => {
         bool isDone = action(www, progressCallback);
         if (!isDone)
         {
             WWWRequestLooper.RegisterNetworkRequest(www, progressCallback, action);
         }
     });
 }
Пример #3
0
        private void ExecSendRequestForDownload(Stream outStream, ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            if (callback == null)
            {
                callback(null, new ArgumentNullException("callback is null"));
                return;
            }
            if (outStream == null)
            {
                callback(null, new ArgumentNullException("outStream is null"));
                return;
            }
            float timeout = Time.realtimeSinceStartup + Timeout;

            this.SetHttpMethodOverride();
            this.SetSDKClientInfo();
            WWWRequestLooper.RunOnMainThread(() => {
                WWWRequestLooper.RegisterNetworkRequest(new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary()), progressCallback, (WWW www, ProgressCallbackHelper progress) => {
                    if (!www.isDone)
                    {
                        if (timeout < Time.realtimeSinceStartup)
                        {
                            DisposeWWW(www);
                            callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                            return(true);
                        }
                        if (progress != null)
                        {
                            progress.NotifyDownloadProgress(www);
                        }
                        return(false);
                    }
                    else
                    {
                        try
                        {
                            Exception e = this.CheckHttpError(www);
                            if (e != null)
                            {
                                callback(null, e);
                                return(true);
                            }
                            ApiResponse response = new ApiResponse();
                            Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
                            this.CopyHttpHeaders(responseHeaders, response);
                            response.Status      = WWWUtils.GetStatusCode(responseHeaders, www.bytes == null ? 204 : 200);
                            response.ContentType = WWWUtils.GetHeader(responseHeaders, "Content-Type");
                            response.ETag        = WWWUtils.GetHeader(responseHeaders, "ETag");

                            response.Body = "";
                            if (www.bytes != null)
                            {
                                BinaryWriter writer = new BinaryWriter(outStream);
                                writer.Write(www.bytes);
                            }
                            callback(response, null);
                            return(true);
                        }
                        catch (Exception e)
                        {
                            callback(null, e);
                            return(true);
                        }
                        finally
                        {
                            DisposeWWW(www);
                        }
                    }
                });
            });
        }