Search Unity

Can't preform HttpWebRequest need to send files from Cloud Build to private server

Discussion in 'Unity Build Automation' started by Naphier, Dec 3, 2018.

  1. Naphier

    Naphier

    Joined:
    Aug 4, 2014
    Posts:
    114
    Hello,
    We're examining the possibility of using Unity Cloud Build with our projects. One of the main requirements is that we need an automated process for distributing builds. I was hoping that the PostExport process would help with that, but I cannot get it to run an HttpWebRequest properly.

    The build log says:
    Code (CSharp):
    1. 1136: [Unity] ERROR: Caught exception invoking method 'PostExport': The remote server returned an error: (403) Forbidden.
    2. 1137: [Unity] Stack trace:
    3. 1138: [Unity]   at System.Net.HttpWebRequest.CheckFinalStatus (System.Net.WebAsyncResult result) [0x00000] in <filename unknown>:0
    4. 1139: [Unity]   at System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData data) [0x00000] in <filename unknown>:0
    5. 1140: [Unity] UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
    6. 1141: [Unity] UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
    7. 1142: [Unity] UnityEngine.Logger:Log(LogType, Object)
    8. 1143: [Unity] UnityEngine.Debug:Log(Object)
    9. 1144: [Unity] UnityEditor.CloudBuild.BuildLogger:Log(String, Object[])
    10. 1145: [Unity] UnityEditor.CloudBuild.UnityReflector:CallStaticEditorMethod(MethodInfo, Boolean, Object[])
    11. 1146: [Unity] UnityEditor.CloudBuild.Builder:TryRunUserExportMethod(String, Object)
    12. 1147: [Unity] UnityEditor.CloudBuild.Builder:Build()
    13. 1148: [Unity] ERROR: postExportMethod 'PostCloudBuildExport.PostExport' failed, aborting.
    Here's the script containing post export:
    Code (CSharp):
    1. using System.Net;
    2. using System.Text;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEditor;
    6. using System.Net.Security;
    7.  
    8. public class PostCloudBuildExport
    9. {
    10.     [MenuItem("Tools/Test PostExport")]
    11.     public static void Test()
    12.     {
    13.         PostExport(@"C:\some\silly path\to test\");
    14.     }
    15.  
    16.     //just url expecting post var 'path' and sends response string and exit(200)
    17.     const string URL = "redacted";
    18.  
    19.     public static void PostExport(string exportPath)
    20.     {
    21.         ServicePointManager.ServerCertificateValidationCallback =
    22.             new RemoteCertificateValidationCallback((a, b, c, d) => { return true; });
    23.  
    24.         var request = (HttpWebRequest)WebRequest.Create(URL);
    25.         var postData = "path=" + exportPath;
    26.         var data = Encoding.ASCII.GetBytes(postData);
    27.  
    28.         request.Method = "POST";
    29.         request.ContentType = "application/x-www-form-urlencoded";
    30.         request.ContentLength = data.Length;
    31.         request.UnsafeAuthenticatedConnectionSharing = true;
    32.         request.KeepAlive = true;
    33.         request.AllowAutoRedirect = true;
    34.  
    35.         using (var stream = request.GetRequestStream())
    36.         {
    37.             stream.Write(data, 0, data.Length);
    38.         }
    39.  
    40.         var response = (HttpWebResponse)request.GetResponse();
    41.         string responseString = "";
    42.         using (var responseStream = new StreamReader(response.GetResponseStream()))
    43.         {
    44.             responseString = responseStream.ReadToEnd();
    45.         }
    46.  
    47.         if (response.StatusCode != HttpStatusCode.OK)
    48.         {
    49.             Debug.LogError("Failed to POST. Response:\r\n" + responseString);
    50.         }
    51.         else
    52.             Debug.Log("Post OK. Response:\r\n" + responseString);
    53.     }
    54. }
    Works like a charm running from the editor, but fails on the cloud server. I've also tested CORS issues by running the request from a separate server. All is OK.

    So... How do we automate distribution of builds that we don't want distributed through an app store?
     
  2. Naphier

    Naphier

    Joined:
    Aug 4, 2014
    Posts:
    114
    Ugh... nevermind. I didn't see they have webhooks set up. That certainly didn't come up in any searches. So in case anyone finds this ever, look in to the project's "Integrations" area. That's where you can define simple webhooks and it will give you a direct d/l link. Cheers!
     
    MechEthan likes this.