public static void Init() { if (singleton != null) { return; } lock ( singletonLock ) { if (singleton != null) { return; } singletonGameObject = new GameObject(); GameObject.DontDestroyOnLoad(singletonGameObject); singleton = singletonGameObject.AddComponent <ResponseCallbackDispatcher>(); singletonGameObject.name = "HTTPResponseCallbackDispatcher"; } }
public virtual void Send(Action <Request> callback = null) { if (!synchronous && callback != null && ResponseCallbackDispatcher.Singleton == null) { ResponseCallbackDispatcher.Init(); } completedCallback = callback; isDone = false; state = RequestState.Waiting; if (acceptGzip) { SetHeader("Accept-Encoding", "gzip"); } if (this.cookieJar != null) { List <Cookie> cookies = this.cookieJar.GetCookies(new CookieAccessInfo(uri.Host, uri.AbsolutePath)); string cookieString = this.GetHeader("cookie"); for (int cookieIndex = 0; cookieIndex < cookies.Count; ++cookieIndex) { if (cookieString.Length > 0 && cookieString[cookieString.Length - 1] != ';') { cookieString += ';'; } cookieString += cookies[cookieIndex].name + '=' + cookies[cookieIndex].value + ';'; } SetHeader("cookie", cookieString); } if (bytes != null && bytes.Length > 0 && GetHeader("Content-Length") == "") { SetHeader("Content-Length", bytes.Length.ToString()); Debugger.Log("content-length->" + bytes.Length.ToString()); } if (GetHeader("User-Agent") == "") { try { SetHeader("User-Agent", "UnityWeb/1.0 (Unity " + Request.unityVersion + "; " + Request.operatingSystem + ")"); } catch (Exception) { SetHeader("User-Agent", "UnityWeb/1.0"); } } if (GetHeader("Connection") == "") { SetHeader("Connection", "close"); } // Basic Authorization if (!String.IsNullOrEmpty(uri.UserInfo)) { SetHeader("Authorization", "Basic " + System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(uri.UserInfo))); } if (synchronous) { GetResponse(); } else { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object t) { GetResponse(); })); } }