예제 #1
0
 protected Exception CheckHttpError(WWW www)
 {
     if (!Utils.IsEmpty(www.error))
     {
         Dictionary <string, string> responseHeaders = WWWUtils.LowerCaseHeaders(www);
         int httpStatus = WWWUtils.GetStatusCode(responseHeaders, 0);
         if (httpStatus == 0)
         {
             StringBuilder log = new StringBuilder();
             log.Append(www.error + " ");
             log.Append(Enum.GetName(typeof(KiiHttpMethod), this.method) + ":");
             log.AppendLine(this.url);
             Hashtable headers = this.headers.GetHeadersAsHashtable();
             foreach (String key in headers.Keys)
             {
                 log.AppendLine(key + ":" + headers[key]);
             }
             return(new NetworkException(new SystemException(log.ToString())));
         }
         else
         {
             // WWW cannot get response body if server returns a error status.
             return(KiiHttpUtils.TypedException(httpStatus, null));
         }
     }
     return(null);
 }
예제 #2
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);
                    }
                }
            });
        }
예제 #3
0
        private void ExecSendRequest(KiiHttpClientProgressCallback progressCallback, KiiHttpClientProgressPercentageCallback progressPercentageCallback, KiiHttpClientCallback callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback is null");
            }
            this.SetSDKClientInfo();
            WWW www = new WWW(this.url, this.body, this.headers.GetHeadersAsDictionary());

            try
            {
                float timeout = Time.realtimeSinceStartup + Timeout;
                while (!www.isDone)
                {
                    if (timeout < Time.realtimeSinceStartup)
                    {
                        callback(null, new NetworkException(new TimeoutException("Connection timeout. (did not finish within " + Timeout + " seconds)")));
                        return;
                    }
                    Thread.Sleep(500);
                    if (progressCallback != null)
                    {
                        progressCallback((long)(this.body.Length * www.uploadProgress), this.body.Length);
                    }
                    if (progressPercentageCallback != null)
                    {
                        progressPercentageCallback(www.progress);
                    }
                }
                Exception e = this.CheckHttpError(www);
                if (e != null)
                {
                    callback(null, e);
                    return;
                }
                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);
            }
            finally
            {
                DisposeWWW(www);
            }
        }
예제 #4
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);
                        }
                    }
                });
            });
        }