/** WEBリクエスト作成。 */ public static UnityEngine.Networking.UnityWebRequest CreateWebRequest(Fee.File.Path a_uri_path, UnityEngine.Networking.CertificateHandler a_certificate) { UnityEngine.Networking.UnityWebRequest t_webrequest = null; try{ t_webrequest = UnityEngine.Networking.UnityWebRequest.Get(a_uri_path.GetPath()); if (a_certificate != null) { t_webrequest.certificateHandler = a_certificate; } UnityEngine.Networking.UnityWebRequestAsyncOperation t_async = t_webrequest.SendWebRequest(); while (t_async.isDone == false) { System.Threading.Thread.Sleep(100); } }catch (System.Exception t_exception) { Tool.EditorLogError(t_exception.Message); t_webrequest = null; } return(t_webrequest); }
/** Get */ public static byte[] Get(string a_url) { try{ using (UnityEngine.Networking.UnityWebRequest t_webrequest = ((System.Func <UnityEngine.Networking.UnityWebRequest>)(() => { return(UnityEngine.Networking.UnityWebRequest.Get(a_url)); }))()){ UnityEngine.Networking.UnityWebRequestAsyncOperation t_async = t_webrequest.SendWebRequest(); while (true) { System.Threading.Thread.Sleep(1); if (t_async.isDone == true) { if (t_webrequest.error != null) { string t_text = ""; if (t_webrequest.downloadHandler.text != null) { t_text = t_webrequest.downloadHandler.text; } UnityEngine.Debug.LogError(a_url + " : " + t_webrequest.error + " : " + t_text); return(null); } else { return(t_webrequest.downloadHandler.data); } } } } }catch (System.Exception t_exception) { UnityEngine.Debug.LogError(a_url + " : " + t_exception.Message + "\n" + t_exception.StackTrace); return(null); } }
/// <summary>Tracks and logs a request upon it completing.</summary> public static void DebugWebRequest(UnityWebRequestAsyncOperation operation, LocalUser userData, int timeSent = -1) { #if DEBUG DebugUtilities.DebugDownload(operation, userData, null, timeSent); #endif // DEBUG }
int UnityEngineNetworkingUnityWebRequest_m_SendWebRequest(RealStatePtr L, int gen_param_count) { ObjectTranslator translator = this; UnityEngine.Networking.UnityWebRequest gen_to_be_invoked = (UnityEngine.Networking.UnityWebRequest)translator.FastGetCSObj(L, 1); { UnityEngine.Networking.UnityWebRequestAsyncOperation gen_ret = gen_to_be_invoked.SendWebRequest( ); translator.Push(L, gen_ret); return(1); } }
/** WebRequest */ public static UnityEngine.Networking.UnityWebRequest WebRequest(string a_uri, UnityEngine.Networking.CertificateHandler a_certificate) { UnityEngine.Networking.UnityWebRequest t_webrequest = UnityEngine.Networking.UnityWebRequest.Get(a_uri); if (a_certificate != null) { t_webrequest.certificateHandler = a_certificate; } UnityEngine.Networking.UnityWebRequestAsyncOperation t_async = t_webrequest.SendWebRequest(); while (t_async.isDone == false) { System.Threading.Thread.Sleep(100); } return(t_webrequest); }
/** ロード。 * * a_url : URL * a_post == null : GET * return : バイナリ。 * */ public static byte[] Load(string a_url, System.Collections.Generic.List <UnityEngine.Networking.IMultipartFormSection> a_post) { using (UnityEngine.Networking.UnityWebRequest t_webrequest = ((System.Func <UnityEngine.Networking.UnityWebRequest>)(() => { if (a_post == null) { return(UnityEngine.Networking.UnityWebRequest.Get(a_url)); } else { return(UnityEngine.Networking.UnityWebRequest.Post(a_url, a_post)); } }))()){ UnityEngine.Networking.UnityWebRequestAsyncOperation t_async = t_webrequest.SendWebRequest(); while (true) { System.Threading.Thread.Sleep(1); if (t_async.isDone == true) { if (t_webrequest.error != null) { #if (DEF_BLUEBACK_ASSETLIB_ASSERT) DebugTool.Assert(false, t_webrequest.error); #endif return(null); } else { byte[] t_binary = t_webrequest.downloadHandler.data; if (t_binary == null) { #if (DEF_BLUEBACK_ASSETLIB_ASSERT) DebugTool.Assert(false, t_webrequest.error); #endif return(null); } else { return(t_binary); } } } } } }
/** CoroutineMain */ public System.Collections.IEnumerator CoroutineMain(Fee.File.OnFileCoroutine_CallBackInterface a_callback_interface, Fee.File.Path a_path, System.Collections.Generic.List <UnityEngine.Networking.IMultipartFormSection> a_post_data, Fee.File.CustomCertificateHandler a_certificate_handler) { //result this.result = null; //ロード。 byte[] t_result_binary = null; System.Collections.Generic.Dictionary <string, string> t_result_responseheader = null; long t_result_responsecode = 0; { using (UnityEngine.Networking.UnityWebRequest t_webrequest = CreateWebRequestInstance(a_path, a_post_data, a_certificate_handler)){ //通信。 { UnityEngine.Networking.UnityWebRequestAsyncOperation t_webrequest_async = null; if (t_webrequest != null) { t_webrequest_async = t_webrequest.SendWebRequest(); if (t_webrequest_async == null) { //エラー。 this.result = new ResultType(null, "Unknown Error : LoadUrlTextFile : " + a_path.GetPath(), t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); yield break; } } else { //エラー。 this.result = new ResultType(null, "Unknown Error : LoadUrlTextFile : " + a_path.GetPath(), t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); yield break; } do { //エラーチェック。 if (t_webrequest.result == UnityEngine.Networking.UnityWebRequest.Result.InProgress) { //実行中。 } else if (t_webrequest.result == UnityEngine.Networking.UnityWebRequest.Result.Success) { //正常終了。 yield return(t_webrequest_async); break; } else { //エラー。 if (t_webrequest.error != null) { this.result = new ResultType(null, "Connect Error : LoadUrlTextFile : " + a_path.GetPath() + " : " + t_webrequest.error, t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); } else { this.result = new ResultType(null, "Connect Error : LoadUrlTextFile : " + a_path.GetPath(), t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); } yield break; } //キャンセルチェック。 { if (a_callback_interface != null) { float t_progress = (t_webrequest.uploadProgress + t_webrequest.downloadProgress) / 2; if (a_callback_interface.OnFileCoroutine(t_progress) == false) { t_webrequest.Abort(); } } } yield return(null); }while(true); } //コンバート。 try{ if (t_webrequest.downloadHandler == null) { //エラー。 this.result = new ResultType(null, "Convert Error : LoadUrlTextFile : " + a_path.GetPath(), t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); yield break; } byte[] t_result = t_webrequest.downloadHandler.data; if (t_result == null) { //エラー。 this.result = new ResultType(null, "Convert Error : LoadUrlTextFile : " + a_path.GetPath(), t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); yield break; } t_result_binary = t_result; }catch (System.Exception t_exception) { //エラー。 this.result = new ResultType(null, "Convert Error : LoadUrlTextFile : " + a_path.GetPath() + " : " + t_exception.Message, t_webrequest.GetResponseHeaders(), t_webrequest.responseCode); yield break; } //レスポンスヘッダー。 t_result_responseheader = t_webrequest.GetResponseHeaders(); t_result_responsecode = t_webrequest.responseCode; } } //コンバート。 string t_result_text = null; { string t_result = Fee.StringConvert.Utf8BinaryToString.Convert(t_result_binary, 0, t_result_binary.Length); if (t_result != null) { //成功。 t_result_text = t_result; } else { //エラー。 this.result = new ResultType(null, "Convert Error : LoadUrlTextFile : " + a_path.GetPath(), t_result_responseheader, t_result_responsecode); yield break; } } //成功。 this.result = new ResultType(t_result_text, null, t_result_responseheader, t_result_responsecode); yield break; }
/// <summary>Tracks and logs a download upon it completing.</summary> public static void DebugDownload(UnityWebRequestAsyncOperation operation, LocalUser userData, string downloadLocation, int timeSent = -1) { #if DEBUG Debug.Assert(operation != null); UnityWebRequest webRequest = operation.webRequest; string userIdString = DebugUtilities.GenerateUserIdString(userData.profile); if (timeSent < 0) { timeSent = ServerTimeStamp.Now; } if (PluginSettings.REQUEST_LOGGING.logOnSend) { var logString = new System.Text.StringBuilder(); logString.AppendLine("[mod.io] Web Request Sent"); logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(downloadLocation); } if (timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(timeSent).ToString()); logString.Append(" ["); logString.Append(timeSent.ToString()); logString.AppendLine("]"); } logString.AppendLine(); string requestString = DebugUtilities.GetRequestInfo(webRequest, userIdString); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); Debug.Log(logString.ToString()); } if (PluginSettings.REQUEST_LOGGING.logAllResponses || PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { RequestDebugData debugData = new RequestDebugData() { userIdString = userIdString, timeSent = timeSent, downloadLocation = downloadLocation, }; DebugUtilities.webRequestDebugData.Add(webRequest, debugData); // handle completion if (operation.isDone) { DebugUtilities.OnOperationCompleted(operation); } else { operation.completed += DebugUtilities.OnOperationCompleted; } } #endif // DEBUG }
/// <summary>Callback upon request operation completion.</summary> private static void OnOperationCompleted(AsyncOperation operation) { if (operation == null) { return; } // get vars UnityWebRequestAsyncOperation o = operation as UnityWebRequestAsyncOperation; UnityWebRequest webRequest = o.webRequest; var now = ServerTimeStamp.Now; bool isError = (webRequest.isNetworkError || webRequest.isHttpError); // should we log? if (PluginSettings.REQUEST_LOGGING.logAllResponses || isError) { RequestDebugData debugData; if (!DebugUtilities.webRequestDebugData.TryGetValue(webRequest, out debugData)) { debugData = new RequestDebugData() { userIdString = "NONE_RECORDED", timeSent = -1, downloadLocation = null, }; } // generate strings string requestString = DebugUtilities.GetRequestInfo(webRequest, debugData.userIdString); string responseString = DebugUtilities.GetResponseInfo(webRequest); // generate log string var logString = new System.Text.StringBuilder(); if (!isError) { logString.AppendLine("[mod.io] Web Request Succeeded"); } else { logString.AppendLine("[mod.io] Web Request Failed"); } logString.Append("URL: "); logString.Append(webRequest.url); logString.Append(" ("); logString.Append(webRequest.method.ToUpper()); logString.AppendLine(")"); if (!string.IsNullOrEmpty(debugData.downloadLocation)) { logString.Append("Download Location: "); logString.AppendLine(debugData.downloadLocation); } if (debugData.timeSent >= 0) { logString.Append("Sent: "); logString.Append(ServerTimeStamp.ToLocalDateTime(debugData.timeSent).ToString()); logString.Append(" ["); logString.Append(debugData.timeSent.ToString()); logString.AppendLine("]"); } logString.Append("Completed: "); logString.Append(ServerTimeStamp.ToLocalDateTime(now).ToString()); logString.Append(" ["); logString.Append(now.ToString()); logString.AppendLine("]"); logString.AppendLine(); logString.AppendLine("------[ Request ]------"); logString.AppendLine(requestString); logString.AppendLine("------[ Response ]------"); logString.AppendLine(responseString); // log if (isError && PluginSettings.REQUEST_LOGGING.errorsAsWarnings) { Debug.LogWarning(logString.ToString()); } else { Debug.Log(logString.ToString()); } } DebugUtilities.webRequestDebugData.Remove(webRequest); }
public static Hidden.UnityWebRequestAwaiter GetAwaiter(this UnityEngine.Networking.UnityWebRequestAsyncOperation asyncOp) { return(new Hidden.UnityWebRequestAwaiter(asyncOp)); }
public UnityWebRequestAwaiter(UnityEngine.Networking.UnityWebRequestAsyncOperation asyncOp) { this.asyncOp = asyncOp; asyncOp.completed += OnRequestCompleted; }