Пример #1
0
        public IEnumerator Post(object request, Dictionary <string, string> extraHeaders, Action <object> callback)
        {
            var webRequest = new UnityWebRequest(OneDsUtility.ONEDS_SERVICE_URL, "POST");

            webRequest.uploadHandler   = new UploadHandlerRaw(request as byte[]);
            webRequest.downloadHandler = new DownloadHandlerBuffer();

            string currentTimestampString = Microsoft.Applications.Events.Utils.MsFrom1970().ToString();

            extraHeaders.Add("sdk-version", "OCT_C#-0.11.1.0");
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
            extraHeaders.Add("Content-Encoding", "gzip");
#endif
            extraHeaders.Add("Content-Type", "application/bond-compact-binary");
            extraHeaders.Add("Upload-Time", currentTimestampString);
            extraHeaders.Add("client-time-epoch-millis", currentTimestampString);
            extraHeaders.Add("Client-Id", "NO_AUTH");

            foreach (var header in extraHeaders)
            {
                webRequest.SetRequestHeader(header.Key, header.Value);
            }

#if UNITY_2017_2_OR_NEWER
            webRequest.chunkedTransfer = false; // can be removed after Unity's PUT will be more stable
            yield return(webRequest.SendWebRequest());
#else
            yield return(webRequest.Send());
#endif
            OneDsUtility.ParseResponse(webRequest.responseCode, () => webRequest.downloadHandler.text, webRequest.error, callback);
        }
Пример #2
0
        private IEnumerator OneDsPost(object request, Dictionary <string, string> extraHeaders, Action <object> callback)
        {
            var payload = request as byte[];

            if (payload == null)
            {
                callback?.Invoke(new PlayFabError
                {
                    Error        = PlayFabErrorCode.Unknown,
                    ErrorMessage = "Request is null."
                });

                yield break;
            }

            var www = new UnityWebRequest(OneDsUtility.ONEDS_SERVICE_URL)
            {
                uploadHandler   = new UploadHandlerRaw(payload),
                downloadHandler = new DownloadHandlerBuffer(),
                method          = UnityWebRequest.kHttpVerbPOST
            };

            string currentTimestampString = Microsoft.Applications.Events.Utils.MsFrom1970().ToString();

            www.SetRequestHeader("sdk-version", "OCT_C#-0.11.1.0");
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
            www.SetRequestHeader("Content-Encoding", "gzip");
#endif
            www.SetRequestHeader("Content-Type", "application/bond-compact-binary");
            www.SetRequestHeader("Upload-Time", currentTimestampString);
            www.SetRequestHeader("client-time-epoch-millis", currentTimestampString);
            www.SetRequestHeader("Client-Id", "NO_AUTH");

            foreach (var header in extraHeaders)
            {
                if (!string.IsNullOrEmpty(header.Key) && !string.IsNullOrEmpty(header.Value))
                {
                    www.SetRequestHeader(header.Key, header.Value);
                }
                else
                {
                    Debug.LogWarning("Null header: " + header.Key + " = " + header.Value);
                }
            }

#if UNITY_2017_2_OR_NEWER
            www.chunkedTransfer = false;
            yield return(www.SendWebRequest());
#else
            yield return(www.Send());
#endif

            using (www)
            {
                OneDsUtility.ParseResponse(www.responseCode, () => www.downloadHandler.text, www.error, callback);
            }
        }
Пример #3
0
        public void DoPost(object request, Dictionary <string, string> extraHeaders, Action <object> callback)
        {
            var thread = new Thread(() =>
            {
                string currentTimestampString = Microsoft.Applications.Events.Utils.MsFrom1970().ToString();
                var httpRequest = (HttpWebRequest)WebRequest.Create(OneDsUtility.ONEDS_SERVICE_URL);

                httpRequest.Method      = "POST";
                httpRequest.ContentType = "application/bond-compact-binary";
                httpRequest.Headers.Add("sdk-version", "OCT_C#-0.11.1.0");
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                httpRequest.Headers.Add("Content-Encoding", "gzip");
#endif
                httpRequest.Headers.Add("Upload-Time", currentTimestampString);
                httpRequest.Headers.Add("client-time-epoch-millis", currentTimestampString);
                httpRequest.Headers.Add("Client-Id", "NO_AUTH");

                foreach (var header in extraHeaders)
                {
                    httpRequest.Headers.Add(header.Key, header.Value);
                }

                var payload = request as byte[];

                if (payload != null)
                {
                    httpRequest.ContentLength = payload.Length;
                    using (var stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(payload, 0, payload.Length);
                    }
                }

                try
                {
                    var response = (HttpWebResponse)httpRequest.GetResponse();

                    OneDsUtility.ParseResponse((long)response.StatusCode, () =>
                    {
                        string json;
                        using (var responseStream = new StreamReader(response.GetResponseStream()))
                        {
                            json = responseStream.ReadToEnd();
                        }
                        return(json);
                    }, null, callback);
                }
                catch (WebException webException)
                {
                    if (callback == null)
                    {
                        return;
                    }

                    try
                    {
                        using (var responseStream = webException.Response.GetResponseStream())
                        {
                            if (responseStream != null)
                            {
                                using (var stream = new StreamReader(responseStream))
                                {
                                    callback.Invoke(new OneDsError
                                    {
                                        Error        = PlayFabErrorCode.Unknown,
                                        ErrorMessage = stream.ReadToEnd()
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        callback.Invoke(new OneDsError
                        {
                            Error        = PlayFabErrorCode.Unknown,
                            ErrorMessage = exception.Message
                        });
                    }
                }
                catch (Exception e)
                {
                    callback.Invoke(new OneDsError
                    {
                        Error        = PlayFabErrorCode.Unknown,
                        ErrorMessage = e.Message
                    });
                }
            });

            thread.Start();
        }