Search Unity

uploading file on server and keep getting an error

Discussion in 'Scripting' started by roshaantariq, Oct 1, 2018.

  1. roshaantariq

    roshaantariq

    Joined:
    Dec 6, 2017
    Posts:
    32
    I'm trying to upload binary data onto server but unity keeps giving me error "could not connect to destination host"

    below is my co-routine code:

    Code (CSharp):
    1. private IEnumerator UploadAssets()
    2.      {
    3.          yield return new WaitForSeconds(5);
    4.          Debug.LogError("starting now");
    5.          string baseURL = "http://config2.gamesparks.net/restv2/game/{myapikey}/config/~downloadables/testingfile/file";
    6.          //string encodedURL = WWW.EscapeURL(baseURL);
    7.          byte[] bytes = File.ReadAllBytes("D:\\testingfile.txt");
    8.          WWWForm form = new WWWForm();
    9.          form.AddField("downloadableId", "testingfile");
    10.          form.AddField("apiKey", "{myapikey}");
    11.          form.AddBinaryData("formData", bytes, "D:\\testingfile.txt", "text/plain");
    12.          using (UnityWebRequest www = UnityWebRequest.Post(baseURL, form))
    13.          {
    14.              www.SetRequestHeader("Authorization", "{myusername/password}");
    15.              www.SetRequestHeader("Accept", "application/json");
    16.              yield return www.SendWebRequest();
    17.              Debug.LogError(www.responseCode);
    18.              Debug.LogError(www.GetResponseHeader("shortCode"));
    19.              if (www.isNetworkError || www.isHttpError)
    20.              {
    21.                  Debug.Log(www.error);
    22.              }
    23.              else
    24.              {
    25.                  Debug.Log("Form upload complete!");
    26.              }
    27.              Debug.LogError("out");
    28.         }
    29.      }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Have you tried accessing the url you're passing? From somewhere like browser?
    Your application may not have permissions for the network (e.g. blocked by Windows Firewall), or simply destination host is not reachable. Or url is incorrect.
     
  3. roshaantariq

    roshaantariq

    Joined:
    Dec 6, 2017
    Posts:
    32
    yes I've tried the exact same thing from browser and it's working