Esempio n. 1
0
        /// <summary>
        /// Set web result content type
        /// </summary>
        /// <param name="contentType">Type type of content you want to load from the web.</param>
        /// <returns></returns>
        public Stackeer SetWebRequestType(WEB_REQUEST_TYPE contentType)
        {
            this.contentType = contentType;

            if (enableLog)
            {
                Debug.Log("[Stackeer] Content type set to : " + contentType);
            }

            return(this);
        }
Esempio n. 2
0
        public WebEasyHttpQy(WEB_REQUEST_TYPE webNetType       = WEB_REQUEST_TYPE.HTTP,
                             WEB_DATA_PROTOCOL webDataProtocol = WEB_DATA_PROTOCOL.ORIGINAL)
        {
            switch (webNetType)
            {
            case WEB_REQUEST_TYPE.HTTP:
                m_WebRequest = new HttpWebRequestQy();
                break;

            default:
                DebugQy.LogError("'WEB_REQUEST_TYPE' can not clear. ");
                break;
            }
            switch (webDataProtocol)
            {
            case WEB_DATA_PROTOCOL.ORIGINAL:
                m_protocol = new WebKeepOrigDataProtocol();
                break;

            default:
                DebugQy.LogError("'WEB_DATA_PROTOCOL' can not clear. ");
                break;
            }
        }
    // Don't declare local variables
    public IEnumerator Request(string url,
                               WEB_REQUEST_TYPE webRequestMethodType,
                               WWWForm wWWForm = null,
                               string query    = null,
                               Action <WebRequestInfo> callBackWithData = null,
                               string userName = null)
    {
        //if (!InternetValidator.Instance.IsInternetAvailable)
        //{
        //    WebRequestInfo info = new WebRequestInfo
        //    {
        //        isInterNetConnectionAvailable = false,
        //        isSuccess = false
        //    };
        //    callBackWithData(info);
        //}
        //else
        {
            switch (webRequestMethodType)
            {
            case WEB_REQUEST_TYPE.POST:
            {
                using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, wWWForm))
                {
                    // unityWebRequest.timeout = (int)ServerConstants.REQUEST_TIMEOUT;

                    // unityWebRequest.chunkedTransfer = true;
                    yield return(unityWebRequest.SendWebRequest());

                    WebRequestInfo info = new WebRequestInfo();

                    // InternetValidator.Instance.IsInternetAvailable = !unityWebRequest.isNetworkError;

                    if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
                    {
                        info.errorDescription = unityWebRequest.error;
                        info.isSuccess        = false;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(unityWebRequest.error) && unityWebRequest.isDone)
                        {
                            info.callBackData = unityWebRequest.downloadHandler.data;

                            //string responseText = Encoding.UTF8.GetString(info.callBackData);

                            info.isSuccess = true;
                        }
                        else
                        {
                            info.errorDescription = unityWebRequest.error;
                            info.isSuccess        = false;
                        }
                    }
                    yield return(new WaitForEndOfFrame());

                    callBackWithData(info);
                }
            }
            break;

            case WEB_REQUEST_TYPE.GET:
            {
                using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(url))
                {
                    unityWebRequest.timeout = (int)ServerConstants.REQUEST_TIMEOUT;

                    yield return(unityWebRequest.SendWebRequest());

                    //Utilities.DebugLogColor("Data progress : " + unityWebRequest.downloadProgress, "red");

                    WebRequestInfo info = new WebRequestInfo();

                    //  InternetValidator.Instance.IsInternetAvailable = !unityWebRequest.isNetworkError;

                    if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
                    {
                        info.errorDescription = unityWebRequest.error;
                        info.isSuccess        = false;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(unityWebRequest.error) && unityWebRequest.isDone)
                        {
                            if (string.IsNullOrEmpty(unityWebRequest.downloadHandler.text))
                            {
                                info.errorDescription = "Content not available";
                                info.isSuccess        = false;
                            }
                            else
                            {
                                info.isSuccess    = true;
                                info.callBackData = unityWebRequest.downloadHandler.data;

                                //string responseText = Encoding.UTF8.GetString(info.callBackData);
                            }
                        }
                        else
                        {
                            info.errorDescription = unityWebRequest.error;
                            info.isSuccess        = false;
                        }
                    }
                    yield return(new WaitForEndOfFrame());

                    callBackWithData(info);
                }
            }
            break;

            case WEB_REQUEST_TYPE.GRAPH_QUERY:
            {
                using (UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST))
                {
                    var fullQuery = new GraphQLQuery()
                    {
                        query = query,
                    };

                    string json = JsonUtility.ToJson(fullQuery);

                    byte[]        payload    = Encoding.UTF8.GetBytes(json);
                    UploadHandler headerData = new UploadHandlerRaw(payload);

                    unityWebRequest.uploadHandler = headerData;
                    //unityWebRequest.SetRequestHeader("Content-Type", "application/json");

                    //    unityWebRequest.SetRequestHeader("username", "example");
                    //unityWebRequest.SetRequestHeader("code", "");

                    //    unityWebRequest.SetRequestHeader("buildtype", ServerGameStateConstants.HEADER_BUILD_TYPE); //1 - Production, 0 - Sandbox

                    //string nonce = GetNonce();
                    //unityWebRequest.SetRequestHeader("nonce", nonce);

                    //string payLoadString = Encoding.UTF8.GetString(payload);
                    //string signature = CalculateMD5Hash(nonce + payLoadString);
                    //unityWebRequest.SetRequestHeader("signature", signature);

                    unityWebRequest.timeout = (int)ServerConstants.REQUEST_TIMEOUT;

                    yield return(unityWebRequest.SendWebRequest());

                    WebRequestInfo info = new WebRequestInfo();

                    // InternetValidator.Instance.IsInternetAvailable = !unityWebRequest.isNetworkError;

                    if (unityWebRequest.responseCode == 504)         // 504 Gateway Timeout error, Unable to reach server.
                    {
                        info.isServerDown = true;
                        callBackWithData(info);
                        yield break;
                    }

                    if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
                    {
                        info.errorDescription = unityWebRequest.error;
                        info.isSuccess        = false;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(unityWebRequest.error) && unityWebRequest.isDone)
                        {
                            string callbackDataInString          = System.Text.Encoding.UTF8.GetString(unityWebRequest.downloadHandler.data);
                            Dictionary <string, object> response = Json.Deserialize(callbackDataInString) as Dictionary <string, object>;
                            if (response.ContainsKey("data"))
                            {
                                foreach (KeyValuePair <string, object> author in response)
                                {
                                    if (author.Key.Contains("errors"))
                                    {
                                        info.errorDescription = "Data Not Available";
                                        info.isSuccess        = false;
                                        break;
                                    }
                                    //  if (string.IsNullOrEmpty(author.Value.ToString()) || author.Value == null)
                                    //{
                                    //    MainGameHandler.Instance.gameSessionManager.DisplayPopUP_withButtons( HCConstants.NO_INTERNET_CONNECTION, HCConstants.Alert, HCConstants.Alert_No_internet);
                                    //}


                                    info.responseObj = author.Value;
                                    foreach (KeyValuePair <string, object> responceKeypair in author.Value as Dictionary <string, object> )
                                    {
                                        string responceValueString = Json.Serialize(responceKeypair.Value as Dictionary <string, object>);
                                        if (!string.IsNullOrEmpty(responceValueString))
                                        {
                                            info.responceDataString = responceValueString;

                                            //MainGameHandler.Instance.copyUrl.addURL(url, query, wWWForm, info.responceDataString);

                                            info.isSuccess = true;
                                        }
                                        else
                                        {
                                            info.errorDescription = "Data Not Available";
                                            info.isSuccess        = false;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                info.errorDescription = "Data Not Available";
                                info.isSuccess        = false;
                            }
                        }
                        else
                        {
                            info.errorDescription = unityWebRequest.error;
                            info.isSuccess        = false;
                        }
                    }
                    yield return(new WaitForEndOfFrame());

                    callBackWithData(info);
                }
            }
            break;
            }
        }
    }