Пример #1
0
        public void SendRequest(KiiHttpClientCallback callback)
        {
            // move request headers to list
            requestHeaderList.Add(headers);
            headers = new MockHttpHeaderList();

            ApiResponse response = new ApiResponse();

            if (responseQueue.Count == 0)
            {
                response.Status = 200;
                response.Body   = "{}";
                callback(response, null);
                return;
            }
            MockResponse mockResponse = responseQueue.Dequeue();
            Exception    e            = mockResponse.Ex;

            if (e != null)
            {
                callback(null, e);
                return;
            }
            response.Status = mockResponse.Status;
            response.Body   = mockResponse.Body;
            response.ETag   = mockResponse.ETag;
            if (mockResponse.StepCount >= 0)
            {
                response.Headers["X-Step-count"] = "" + mockResponse.StepCount;
            }
            callback(response, null);
        }
        public virtual void SendRequest(KiiHttpClientCallback callback)
        {
            SetSDKClientInfo();
            try
            {
                HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

                ApiResponse res = ParseResponseHeader(httpResponse);
                // read body
                res.Body = ReadBodyFromResponse(httpResponse);

                callback(res, null);
            }
            catch (System.Net.WebException e)
            {
                Console.Write("Exception " + e.Message);
                System.Net.HttpWebResponse err = (System.Net.HttpWebResponse)e.Response;
                // read body
                string body = ReadBodyFromResponse(err);

                callback(null, KiiHttpUtils.TypedException((int)err.StatusCode, body));
            }
            catch (Exception e)
            {
                callback(null, e);
            }
        }
        public virtual void SendRequest(string body, KiiHttpClientCallback callback)
        {
            if (Kii.Logger != null)
            {
                Kii.Logger.Debug("request body : {0}", body);
            }
            byte[] data = enc.GetBytes(body);
            request.ContentLength = data.Length;

            System.IO.Stream reqStream = null;
            try
            {
                reqStream = request.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
            }
            catch (SystemException e)
            {
                callback(null, new NetworkException(e));
                return;
            }
            finally
            {
                if (reqStream != null)
                {
                    reqStream.Close();
                }
            }

            SendRequest(callback);
        }
Пример #4
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);
                    }
                }
            });
        }
Пример #5
0
 private void _SendRequest(KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.SetHttpMethodOverride();
     this.ExecSendRequest(null, null, callback);
 }
Пример #6
0
 private void _SendRequest(KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.SetHttpMethodOverride();
     WWWRequestLooper.RunOnMainThread(() => {
         this.ExecSendRequest(null, callback);
     });
 }
 public void SendRequest(KiiHttpClientCallback callback)
 {
     try
     {
         ApiResponse response = SendRequest();
         callback(response, null);
     }
     catch (Exception e)
     {
         callback(null, e);
     }
 }
Пример #8
0
 public virtual void SendRequest(string body, KiiHttpClientCallback callback)
 {
     this.Body = body;
     this._SendRequest(callback);
 }
Пример #9
0
 public virtual void SendRequest(KiiHttpClientCallback callback)
 {
     this._SendRequest(callback);
 }
Пример #10
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);
            }
        }
Пример #11
0
 public override void SendRequest(Stream body, KiiHttpClientProgressCallback progressCallback, KiiHttpClientCallback callback)
 {
     System.Threading.ThreadPool.QueueUserWorkItem((object stateInfo) => {
         base.SendRequest(body, progressCallback, callback);
     });
 }
        private void SendRequest(Stream body, ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            System.IO.Stream reqStream = null;
            try
            {
                long doneByte  = 0;
                long totalByte = body.Length;
                request.ContentLength = totalByte;

                reqStream = request.GetRequestStream();

                byte[] buffer = new byte[4096];
                int    count;
                while (true)
                {
                    count = body.Read(buffer, 0, buffer.Length);
                    if (count <= 0)
                    {
                        break;
                    }
                    reqStream.Write(buffer, 0, count);
                    doneByte += count;
                    if (doneByte > totalByte)
                    {
                        doneByte = totalByte;
                    }
                    progressCallback.NotifyProgress(doneByte, totalByte);
                }
            }
            catch (SystemException e)
            {
                callback(null, new NetworkException(e));
                return;
            }
            finally
            {
                if (reqStream != null)
                {
                    reqStream.Close();
                }
            }

            SendRequest(callback);
        }
Пример #13
0
 public void SendRequest(string body, KiiHttpClientCallback callback)
 {
     requestBodyList.Add(body);
     SendRequest(callback);
 }
Пример #14
0
 public override void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     WWWRequestLooper.RunOnMainThread(() => {
         // TODO:If huge file will be uploaded, this way is bad performance.
         // Try to Upload an Object Body in Multiple Pieces
         // See:http://documentation.kii.com/en/guides/rest/managing-data/object-storages/uploading/
         this.ExecSendRequest(new ProgressCallbackHelper(progressCallback), callback);
     });
 }
Пример #15
0
 public override void SendRequest(KiiHttpClientCallback callback)
 {
     this._SendRequest(callback);
 }
        private void SendRequestForDownload(Stream outStream, ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            SetSDKClientInfo();
            try
            {
                HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

                ApiResponse res = ParseResponseHeader(httpResponse);
                // read body
                ReadBodyStream(httpResponse, progressCallback, outStream);
                res.Body = "";

                callback(res, null);
            }
            catch (System.Net.WebException e)
            {
                Console.Write("Exception " + e.Message);
                System.Net.HttpWebResponse err = (System.Net.HttpWebResponse)e.Response;
                // read body
                string body = ReadBodyFromResponse(err);

                callback(null, KiiHttpUtils.TypedException((int)err.StatusCode, body));
            }
            catch (Exception e)
            {
                callback(null, e);
            }
        }
 public virtual void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     SendRequestForDownload(outStream, new ProgressCallbackHelper(progressCallback), callback);
 }
Пример #18
0
        public void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
        {
            ApiResponse response = new ApiResponse();

            if (responseQueue.Count == 0)
            {
                response.Status = 200;
                response.Body   = "{}";
                callback(response, null);
                return;
            }
            MockResponse mockResponse = responseQueue.Dequeue();

            Exception e = mockResponse.Ex;

            if (e != null)
            {
                callback(null, e);
                return;
            }
            byte[] bytes = mockResponse.BytesBody;

            // simulate calling progress callback
            long totalByte = bytes.Length;
            int  count     = numProgressQueue.Dequeue();
            long blockByte = totalByte / count;

            for (int i = 1; i < count; ++i)
            {
                progressCallback((float)((blockByte * i) / totalByte));
            }
            progressCallback((float)(totalByte / totalByte));
            outStream.Write(bytes, 0, (int)totalByte);

            response.Status      = mockResponse.Status;
            response.ContentType = mockResponse.BodyContentType;
            response.ETag        = mockResponse.ETag;

            callback(response, null);
        }
Пример #19
0
 public override void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     this.ExecSendRequestForDownload(outStream, new ProgressCallbackHelper(progressCallback), callback);
 }
 public virtual void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     SendRequest(body, new ProgressCallbackHelper(progressCallback), callback);
 }
Пример #21
0
        public void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
        {
            // simulate calling progress callback
            long totalByte = body.Length;
            int  count     = numProgressQueue.Dequeue();
            long blockByte = totalByte / count;

            for (int i = 1; i < count; ++i)
            {
                progressCallback((float)((blockByte * i) / totalByte));
            }
            progressCallback((float)(totalByte / totalByte));

            SendRequest(callback);
        }
Пример #22
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);
                        }
                    }
                });
            });
        }
Пример #23
0
 public override void SendRequest(string body, KiiHttpClientCallback callback)
 {
     System.Threading.ThreadPool.QueueUserWorkItem((object stateInfo) => {
         base.SendRequest(body, callback);
     });
 }
Пример #24
0
 public virtual void SendRequest(Stream body, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     if (callback == null)
     {
         throw new ArgumentNullException("callback is null");
     }
     this.body = ReadStream(body);
     this.SetHttpMethodOverride();
     this.ExecSendRequest(null, progressCallback, callback);
 }
Пример #25
0
 public override void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     System.Threading.ThreadPool.QueueUserWorkItem((object stateInfo) => {
         base.SendRequestForDownload(outStream, progressCallback, callback);
     });
 }
Пример #26
0
 public virtual void SendRequestForDownload(Stream outStream, KiiHttpClientProgressPercentageCallback progressCallback, KiiHttpClientCallback callback)
 {
     this.ExecSendRequestForDownload(outStream, null, progressCallback, callback);
 }