// Return UnityWebRequest instance private static object asyncRequest(string method, string url, string api, string token, object postObject) { Type unityWebRequestType = UnityWebRequestType(); object request = Activator.CreateInstance(unityWebRequestType, url + api, method); if (postObject != null) { string postData = HandlePostData(JsonUtility.ToJson(postObject)); byte[] postDataBytes = Encoding.UTF8.GetBytes(postData); // Set UploadHandler // Equivalent : request.uploadHandler = (UploadHandler) new UploadHandlerRaw(postDataBytes); Type uploadHanlderRawType = UDPReflectionUtils.GetTypeByName(kUploadHandlerRawTypeString); var uploadHandlerRaw = Activator.CreateInstance(uploadHanlderRawType, postDataBytes); PropertyInfo uploadHandlerInfo = unityWebRequestType.GetProperty("uploadHandler", UDPReflectionUtils.k_InstanceBindingFlags); uploadHandlerInfo.SetValue(request, uploadHandlerRaw, null); } // Set up downloadHandler // Equivalent: request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); var downloadHandlerInstance = Activator.CreateInstance(UDPReflectionUtils.GetTypeByName(kDownloadHandlerBufferTypeString)); var downloadHandlerProperty = unityWebRequestType.GetProperty("downloadHandler", UDPReflectionUtils.k_InstanceBindingFlags); downloadHandlerProperty.SetValue(request, downloadHandlerInstance, null); // Set up header // Equivalent : request.SetRequestHeader("key", "value"); MethodInfo setRequestHeaderMethodInfo = unityWebRequestType.GetMethod("SetRequestHeader", UDPReflectionUtils.k_InstanceBindingFlags); setRequestHeaderMethodInfo.Invoke(request, new object[] { kContentType, kApplicationJson }); if (token != null) { setRequestHeaderMethodInfo.Invoke(request, new object[] { kAuthHeader, "Bearer " + token }); } // Send Web Request // Equivalent: request.SendWebRequest()/request.Send() MethodInfo sendWebRequest = unityWebRequestType.GetMethod("SendWebRequest"); if (sendWebRequest == null) { sendWebRequest = unityWebRequestType.GetMethod("Send"); } sendWebRequest.Invoke(request, null); return(request); }
// UnityWebRequest.DownloadHandler.text internal static string UnityWebRequestResultString(object request) { var downloadHandlerProperty = UnityWebRequestType().GetProperty("downloadHandler", UDPReflectionUtils.k_InstanceBindingFlags); object downloadHandler = downloadHandlerProperty.GetValue(request, null); var textProperty = UDPReflectionUtils.GetTypeByName(kDownloadHandlerBufferTypeString) .GetProperty("text", UDPReflectionUtils.k_InstanceBindingFlags); return((string)textProperty.GetValue(downloadHandler, null)); }
internal static Type UnityWebRequestType() { return(UDPReflectionUtils.GetTypeByName(kUnityWebRequestTypeString)); }
// Using UnityOAuth through reflection to avoid error on Unity lower than 5.6.1. internal static Type GetUnityOAuthType() { return(UDPReflectionUtils.GetTypeByName(kUnityOAuthNamespace)); }