Example #1
0
        private HttpStatusCode ReadStatusCode(WWWResult result)
        {
            _logger.LogDebug("Reading status code...");

            if (result.ResponseHeaders == null || !result.ResponseHeaders.ContainsKey("STATUS"))
            {
                // Based on tests, if response doesn't contain status it has probably timed out.
                _logger.LogWarning("Response is missing STATUS header. Marking it as timed out.");
                throw new WebException("Timeout.", WebExceptionStatus.Timeout);
            }

            var status = result.ResponseHeaders["STATUS"];

            _logger.LogTrace("status = " + status);

            var s = status.Split(' ');

            int statusCode;

            if (s.Length < 3 || !int.TryParse(s[1], out statusCode))
            {
                _logger.LogWarning("Response has invalid status. Marking it as timed out.");
                throw new WebException("Timeout.", WebExceptionStatus.Timeout);
            }

            _logger.LogTrace("statusCode = " + statusCode);
            _logger.LogTrace("statusCode (as enum) = " + (HttpStatusCode)statusCode);

            return((HttpStatusCode)statusCode);
        }
        public IHttpResponse Post(HttpPostRequest postRequest)
        {
            try
            {
                var result = new WWWResult();

                using (var www = new WWW(postRequest.Address.ToString(), Encoding.UTF8.GetBytes(postRequest.FormData)))
                {
                    while (!www.isDone)
                    {
                        // Wait
                    }

                    result.IsDone          = www.isDone;
                    result.ResponseHeaders = www.responseHeaders;
                    result.Text            = www.text;
                }

                lock (result)
                {
                    if (!result.IsDone)
                    {
                        throw new WebException("Timeout after " + postRequest.Timeout, WebExceptionStatus.Timeout);
                    }

                    var statusCode = ReadStatusCode(result);

                    return(new UnityHttpResponse(result.Text, statusCode, ResponseEncoding));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #3
0
        private IEnumerator StartWWW_Internal <DataType>(MonoBehaviour scheduler, string url, OnWWWHandler <DataType> onWWWHandler, float time, params object[] callbackArgs) where DataType : IWWWDataType, new()
        {
            WWWResult result = WWWResult.Failed;

            for (int i = 0; i < RetryCount; ++i)
            {
                if (null != m_www)
                {
                    m_www.Dispose();
                    m_www = null;
                }

                m_www = new WWW(url);
                // Start Timer
                scheduler.StartCoroutine(WaitWWW <DataType>(url, time, onWWWHandler, callbackArgs));

                yield return(m_www);

                if (!Timeout)
                {
                    if (m_www.error != null)
                    {
                        Debug.LogWarning(string.Format("[WWWQuery] failed #{0}({1}): {2}", i, m_www.error, url));
                        result = WWWResult.Failed;
                    }
                    else
                    {
                        result = WWWResult.Success;
                        break;
                    }
                }

                yield return(new WaitForSeconds(3.0f));
            }

            if (!Timeout)
            {
                if (onWWWHandler != null)
                {
                    DataType wwwData = new DataType();
                    wwwData.GetDataFromWWW(result, m_www);
                    onWWWHandler(result, wwwData, callbackArgs);
                }
            }

            if (null != m_www)
            {
                m_www.Dispose();
                m_www = null;
            }
        }
Example #4
0
    public void ResponseCallback(WWWResult www)
    {
        Network.RemoveAPI();
        if (Network.IsError)
        {
            return;
        }
        WebAPI.JSON_BodyResponse <Json_GachaResult> jsonObject = JSONParser.parseJSONObject <WebAPI.JSON_BodyResponse <Json_GachaResult> >(www.text);
        string eventName = string.Empty;

        for (int index = 0; index < jsonObject.body.add.Length; ++index)
        {
            eventName = eventName + jsonObject.body.add[index].iname + ",";
        }
        WatchManager.sendDataToWatch(eventName);
    }
Example #5
0
        private IEnumerator GetWWW(HttpGetRequest getRequest, WWWResult result)
        {
            var www = new WWW(getRequest.Address.ToString());

            yield return(www);

            lock (result)
            {
                result.IsDone = www.isDone;

                if (www.isDone)
                {
                    result.ResponseHeaders = www.responseHeaders;
                    result.Text            = www.text;
                }
            }
        }
        public IHttpResponse Get(HttpGetRequest getRequest)
        {
            try
            {
                if (getRequest.Range != null)
                {
                    throw new NotImplementedException();
                }

                var result = new WWWResult();

                using (var www = new WWW(getRequest.Address.ToString()))
                {
                    while (!www.isDone)
                    {
                        // Wait
                    }

                    result.IsDone          = www.isDone;
                    result.ResponseHeaders = www.responseHeaders;
                    result.Text            = www.text;
                }

                lock (result)
                {
                    if (!result.IsDone)
                    {
                        throw new WebException(
                                  "Timeout after " + getRequest.Timeout,
                                  WebExceptionStatus.Timeout);
                    }

                    var statusCode = ReadStatusCode(result);

                    return(new UnityHttpResponse(
                               result.Text,
                               statusCode,
                               ResponseEncoding));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #7
0
    private IEnumerator LoadRemoteConfig()
    {
        var r = new WWWResult();

        yield return(Loader.TryLoadFile(PLUGINS_ADDRESS_REMOTE, r));

        if (!r.Success)
        {
            EditorUtility.DisplayDialog("Error", r.GetFullError(), "Ok");
            yield break;
        }

        var text = r.GetResult();

        _pluginsRemote = JsonConvert.DeserializeObject <PluginsFile>(text);

        _status = Status.Ready;
    }
        private HttpStatusCode ReadStatusCode(WWWResult result)
        {
            if (result.ResponseHeaders == null || !result.ResponseHeaders.ContainsKey("STATUS"))
            {
                // Based on tests, if response doesn't contain status it has probably timed out.
                throw new WebException("Timeout.", WebExceptionStatus.Timeout);
            }

            var status = result.ResponseHeaders["STATUS"];

            var s = status.Split(' ');

            int statusCode;

            if (s.Length < 3 || !int.TryParse(s[1], out statusCode))
            {
                throw new WebException("Timeout.", WebExceptionStatus.Timeout);
            }

            return((HttpStatusCode)statusCode);
        }
Example #9
0
        public IHttpResponse Get(HttpGetRequest getRequest)
        {
            try
            {
                _logger.LogDebug("Sending GET request to " + getRequest.Address);

                if (getRequest.Range != null)
                {
                    throw new NotImplementedException();
                }

                _logger.LogTrace("timeout  = " + getRequest.Timeout);

                var result = new WWWResult();

                var waitHandle = UnityDispatcher.InvokeCoroutine(GetWWW(getRequest, result));

                waitHandle.WaitOne(TimeSpan.FromMilliseconds(getRequest.Timeout));

                lock (result)
                {
                    if (!result.IsDone)
                    {
                        throw new WebException("Timeout.", WebExceptionStatus.Timeout);
                    }

                    var statusCode = ReadStatusCode(result);

                    _logger.LogDebug("Successfuly received response.");
                    return(new UnityHttpResponse(result.Text, statusCode, ResponseEncoding));
                }
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to get response.", e);
                throw;
            }
        }
 public override void OnSuccess(WWWResult www)
 {
 }