Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question "Cannot connect to destination host" error but server is working

Discussion in 'Scripting' started by cadburia, Dec 31, 2022.

  1. cadburia

    cadburia

    Joined:
    Sep 22, 2022
    Posts:
    9
    Hi,

    Trying to send a post request with an audio file to a server running locally on my computer, but I get a "Cannot connect to destination host" error.

    I double checked that the server is working and that I've got the correct URI.

    I am wondering if maybe how I am trying to add my audio clip to the form is where the issue is? I am stumped so any help would be appreciated! Thanks!


    Code (CSharp):
    1. public void initiateCoroutine(AudioClip audioClip)
    2.     {
    3.         StartCoroutine(postRequest(audioClip));
    4.     }
    5.  
    6.     IEnumerator postRequest(AudioClip audioClip)
    7.     {
    8.         // convert audio clip into a float array
    9.         float[] floatArray = new float[audioClip.samples * audioClip.channels];
    10.         _ = audioClip.GetData(floatArray, 0);
    11.  
    12.         // create a byte array and convert float numbers into chunks of bytes
    13.         var byteArray = new byte[floatArray.Length * 4];
    14.         Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);
    15.  
    16.         // create form and add audio clip as byte array to it
    17.         WWWForm form = new WWWForm();
    18.         form.AddBinaryData("file", byteArray);
    19.  
    20.         UnityWebRequest uwr = UnityWebRequest.Post("https://localhost:5000", form);
    21.         yield return uwr.SendWebRequest();
    22.  
    23.         if (uwr.result == UnityWebRequest.Result.ConnectionError)
    24.         {
    25.             Debug.Log("Error While Sending: " + uwr.error);
    26.         }
    27.         else
    28.         {
    29.             Debug.Log("Received: " + uwr.downloadHandler.text);
    30.         }
    31. }
     
    Last edited: Jan 1, 2023
  2. cadburia

    cadburia

    Joined:
    Sep 22, 2022
    Posts:
    9
    I'd like to delete this thread and come back and post with a better question