Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UnityWebRequest is not working on android phone

Discussion in 'Scripting' started by orizvida, Jan 15, 2021.

  1. orizvida

    orizvida

    Joined:
    Aug 9, 2018
    Posts:
    21
    Hey all,
    I have a PHP file i need to call from my server with some parameters, I am trying to do it using UnityWebRequest but it does not work, I have tried adding to the PHP
    Code (CSharp):
    1. header("Access-Control-Allow-Origin: *");
    but It didn't work.
    It seems that it get stuck before the creation of the UnityWebRequest in the enumarator.
    In editor it works fine but on my android device it does not work at all

    Code (CSharp):
    1.  IEnumerator SetPin()
    2.     {
    3.         DebugText.text = "in Enumarator";
    4.         Debug.Log("in enumarator");
    5.         WWWForm form = new WWWForm();
    6.         form.AddField("value", my_value);
    7.         UnityWebRequest www = UnityWebRequest.Post("https://myadress + "/myphp.php", form);
    8.        www.chunkedTransfer = false;
    9.        DebugText.text = "before sendwebrequest";
    10.  
    11.        yield return www.SendWebRequest();
    12.        if (www.isNetworkError || www.isHttpError)
    13.        {
    14.            Debug.Log("Form Upload Failed");
    15.            DebugText.text = "in error of webrequest";
    16.        }
    17.        else
    18.        {
    19.            Debug.Log("Form Upload Complete");
    20.            DebugText.text = "wev request sent";
    21.        }
    22.    }
    Can anyone help in this? i am trying everything for two days now and no luck.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Have you tried looking into logcat? Maybe an exception is thrown, for example due to invalid URL?
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,920
    You use https. Do you have a valid and signed SSL certificate on your server? AFAIK Android did not trust self-signed certificates as they essentially provide no indentity security and would still allow man-in-the-middle attacks. I would recommend reading Googles Android documentation on SSL security.

    The easiest solution is to actually use a signed certificate. With LetsEncrypt you can aquire a signed certificate for free. Those certificates need to be refreshed every couple of month as they expire after about 3 month, though they are free. On a linux based server it's easy to setup a cron job to refresh the certificate automatically. I use such a certificate on my raspberry pi server with my also free dyndns host name and it usually works everywhere.

    Troubleshooting SSL issues can be quite tiresome. The used certificate on the server need to include the required intermediate certificates as well. There are several pages on the net which can verify your certificate chain simply by providing your server's URL (assuming you have installed your certificate already). Some pages can generate a combined public certificate which includes the necessary intermediate certs as well. Some browsers / TLS clients do not need the intermediate certs as they try to figure them out themselfs. However many plain TLS sockets usually choke on missing links in the chain.

    Unfortunately I'm not home and don't have my link collection at hand, so I can't recommend any pages at the moment.