コード例 #1
0
ファイル: HttpSystem.cs プロジェクト: zhangxinyu1120/Samples
        /// <summary>
        /// <para>The coroutine for processing the HTTP request. This will yield until the
        /// request has completed then parse the information required by a HTTP response
        /// from the WWW object.</para>
        /// </summary>
        ///
        /// <returns>The coroutine enumerator.</returns>
        ///
        /// <param name="www">The WWW object.</param>
        /// <param name="callback">The callback providing the response from the server.</param>
        private IEnumerator ProcessRequest(WWW www, Action <HttpResponse> callback)
        {
            ReleaseAssert.IsTrue(www != null, "The WWW must not be null when sending a request.");
            ReleaseAssert.IsTrue(callback != null, "The callback must not be null when sending a request.");

            yield return(www);

            HttpResponseDesc desc = null;

            if (string.IsNullOrEmpty(www.error))
            {
                ReleaseAssert.IsTrue(www.responseHeaders != null, "A successful HTTP response must have a headers object.");

                desc         = new HttpResponseDesc(HttpResult.Success);
                desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                if (www.bytes != null)
                {
                    desc.Body = www.bytes;
                }

                var httpStatus = www.responseHeaders ["STATUS"];
                ReleaseAssert.IsTrue(!string.IsNullOrEmpty(httpStatus), "A successful HTTP response must have a HTTP status value in the header.");

                desc.HttpResponseCode = ParseHttpStatus(httpStatus);
            }
            else
            {
                int httpResponseCode = ParseHttpError(www.error);
                if (httpResponseCode != 0)
                {
                    desc         = new HttpResponseDesc(HttpResult.Success);
                    desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                    if (www.bytes != null)
                    {
                        desc.Body = www.bytes;
                    }

                    desc.HttpResponseCode = httpResponseCode;
                }
                else
                {
                    desc = new HttpResponseDesc(HttpResult.CouldNotConnect);
                }
            }

            HttpResponse response = new HttpResponse(desc);

            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
                callback(response);
            });
        }
コード例 #2
0
        /// <summary>
        /// Initialises a new instance of a HTTP response with the given description.
        /// </summary>
        ///
        /// <param name="desc">The HTTP response description.</param>
        public HttpResponse(HttpResponseDesc desc)
        {
            ReleaseAssert.IsTrue(desc != null, "The description of a HTTP response must not be null.");
            ReleaseAssert.IsTrue(desc.Headers != null, "The headers of a HTTP response must not be null.");
            ReleaseAssert.IsTrue(desc.Body != null, "The body of a HTTP response must not be null.");

            Result           = desc.Result;
            HttpResponseCode = desc.HttpResponseCode;
            Headers          = desc.Headers;
            Body             = desc.Body;
        }
コード例 #3
0
ファイル: HttpSystem.cs プロジェクト: zhangxinyu1120/Samples
        /// <summary>
        /// <para>The coroutine for processing the HTTP request. This will yield until the
        /// request has completed then get the data from the Web Request object.</para>
        /// </summary>
        ///
        /// <returns>The coroutine enumerator.</returns>
        ///
        /// <param name="webRequest">The Web Request object.</param>
        /// <param name="callback">The callback providing the response from the server.</param>
        private IEnumerator ProcessRequest(UnityWebRequest webRequest, Action <HttpResponse> callback)
        {
            ReleaseAssert.IsTrue(webRequest != null, "The webRequest must not be null when sending a request.");
            ReleaseAssert.IsTrue(callback != null, "The callback must not be null when sending a request.");

            yield return(webRequest.Send());

            HttpResponseDesc desc = null;
            int responseCode      = (int)webRequest.responseCode;

            if (webRequest.isNetworkError)
            {
                // Print error
                UnityEngine.Debug.LogErrorFormat("error = {0}", webRequest.error);
            }

            if (responseCode == 0)
            {
                desc = new HttpResponseDesc(HttpResult.CouldNotConnect);
            }
            else
            {
                desc = new HttpResponseDesc(HttpResult.Success);
            }

            // Populate the request response
            if (webRequest.GetResponseHeaders() == null)
            {
                desc.Headers = new Dictionary <string, string>();
            }
            else
            {
                desc.Headers = new Dictionary <string, string>(webRequest.GetResponseHeaders());
            }

            desc.HttpResponseCode = responseCode;

            // Fill the response data
            if (webRequest.downloadedBytes > 0)
            {
                desc.Body = webRequest.downloadHandler.data;
            }

            HttpResponse response = new HttpResponse(desc);

            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
                callback(response);
            });
        }
コード例 #4
0
        /// <summary>
        /// <para>Processes a web request that has been sent succuessfully, i.e. a
        /// request with a reponse code that is not 503 or has been retried the specified
        /// number of times.</para>
        /// </summary>
        ///
        /// <param name="webRequest">The Web Request object that has been sent.</param>
        /// <param name="responseCode">The Web response code from the web request.</param>
        /// <param name="callback">The callback providing the response from the server.</param>
        private void ProcessSentRequest(UnityWebRequest webRequest, int responseCode, Action <HttpResponse> callback)
        {
            HttpResponseDesc desc = null;

            if (responseCode <= 0)
            {
                desc = new HttpResponseDesc(HttpResult.CouldNotConnect);
            }
            else
            {
                desc = new HttpResponseDesc(HttpResult.Success);
            }

            // Populate the request response
            if (webRequest.GetResponseHeaders() == null)
            {
                desc.Headers = new Dictionary <string, string>();
            }
            else
            {
                desc.Headers = new Dictionary <string, string>(webRequest.GetResponseHeaders());
            }

            desc.HttpResponseCode = responseCode;

            // Fill the response data
            if (webRequest.downloadedBytes > 0)
            {
                desc.Body = webRequest.downloadHandler.data;
            }

            HttpResponse response = new HttpResponse(desc);

            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
                callback(response);
            });
        }
コード例 #5
0
ファイル: HttpSystem.cs プロジェクト: zhangxinyu1120/Samples
        /// <summary>
        /// <para>The coroutine for processing the HTTP request. This will yield until the
        /// request has completed then parse the information required by a HTTP response
        /// from the WWW object.</para>
        /// </summary>
        ///
        /// <returns>The coroutine enumerator.</returns>
        ///
        /// <param name="www">The WWW object.</param>
        /// <param name="callback">The callback providing the response from the server.</param>
        private IEnumerator ProcessRequest(WWW www, Action <HttpResponse> callback)
        {
            ReleaseAssert.IsTrue(www != null, "The WWW must not be null when sending a request.");
            ReleaseAssert.IsTrue(callback != null, "The callback must not be null when sending a request.");

            yield return(www);

            HttpResponseDesc desc = null;

            if (string.IsNullOrEmpty(www.error))
            {
                ReleaseAssert.IsTrue(www.responseHeaders != null, "A successful HTTP response must have a headers object.");

                desc         = new HttpResponseDesc(HttpResult.Success);
                desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                if (www.bytes != null)
                {
                    desc.Body = www.bytes;
                }

                var httpStatus = www.responseHeaders ["STATUS"];
                ReleaseAssert.IsTrue(!string.IsNullOrEmpty(httpStatus), "A successful HTTP response must have a HTTP status value in the header.");

                desc.HttpResponseCode = ParseHttpStatus(httpStatus);
            }
            else
            {
                var bytes            = www.bytes;
                int httpResponseCode = 0;

                                #if UNITY_IPHONE && !UNITY_EDITOR
                var text = www.text;
                if (!string.IsNullOrEmpty(text))
                {
                    var bodyDictionary = Json.Deserialize(text) as Dictionary <string, object>;
                    if (bodyDictionary != null && bodyDictionary.ContainsKey("HttpCode"))
                    {
                        httpResponseCode = Convert.ToInt32(bodyDictionary ["HttpCode"]);
                        bytes            = Encoding.UTF8.GetBytes(text);
                    }
                }
                                #else
                httpResponseCode = ParseHttpError(www.error);
                                #endif

                if (httpResponseCode != 0)
                {
                    desc         = new HttpResponseDesc(HttpResult.Success);
                    desc.Headers = new Dictionary <string, string>(www.responseHeaders);

                    if (www.bytes != null)
                    {
                        desc.Body = www.bytes;
                    }

                    desc.HttpResponseCode = httpResponseCode;
                }
                else
                {
                    desc = new HttpResponseDesc(HttpResult.CouldNotConnect);
                }
            }

            HttpResponse response = new HttpResponse(desc);
            m_taskScheduler.ScheduleBackgroundTask(() =>
            {
                callback(response);
            });
        }