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

Bug ;Unitywebrequest connectionError

Discussion in 'Scripting' started by ayildiiz, May 15, 2023.

  1. ayildiiz

    ayildiiz

    Joined:
    Feb 19, 2021
    Posts:
    5
    Hello everyone,

    I am using unitywebreques on Unity. It works on my android windows and mac devices, but the connection error returns on the iphoe phone, what could be the reason? Unity version:2021.3.4f1
    Code (CSharp):
    1. private IEnumerator CheckFriendNameRoutine(string friendName)
    2.         {
    3.          
    4.             /*
    5.              * Create the request
    6.              */
    7.        
    8.             UnityWebRequest request = UnityWebRequest.Get($"https://*****/****/rest/****/GetUserInformationWithName?UserName={friendName}");
    9.  
    10.             //{
    11.             /*
    12.              * Setup request data
    13.              */
    14.             byte[] requestContent = System.Text.Encoding.UTF8.GetBytes(friendName);
    15.                 request.uploadHandler = new UploadHandlerRaw(requestContent);
    16.                 request.downloadHandler = new DownloadHandlerBuffer();
    17.                 request.SetRequestHeader("Content-Type", "application/json");
    18.  
    19.                 /*
    20.                  * Wait for request
    21.                  */
    22.                 yield return request.SendWebRequest();
    23.              
    24.      
    25.    
    26.  
    27.             if (request.result == UnityWebRequest.Result.Success)
    28.                 {
    29.                
    30.                 PlayerInfo foundPlayerdata = JsonUtility.FromJson<PlayerInfo>(request.downloadHandler.text);
    31.                 Debug.LogError("ArkadasEkleme request error " + foundPlayerdata.UserName);
    32.                     //_foundUserText.text = foundPlayerdata.Id == 0 ? "No user!" : foundPlayerdata.UserName;
    33.  
    34.                     if (foundPlayerdata.Id == 0)
    35.                         _foundUserText.text = "No User!";
    36.                     else
    37.                         _foundUserText.text = foundPlayerdata.UserName;
    38.  
    39.                     _requestButton.interactable = foundPlayerdata.Id != 0;
    40.  
    41.                     if (foundPlayerdata.Id != 0)
    42.                     {
    43.                         _playerAvatar.SetActive(true);
    44.                     }
    45.  
    46.  
    47.                     _lastSearchInfo = foundPlayerdata.Id == 0 ? null : foundPlayerdata;
    48.  
    49.                 }
    50.                 else
    51.                 {
    52.                     Debug.LogError($"Couldnt check friend usernamet due to: {request.result}");
    53.                 }
    54.                 request.Dispose();
    55.  
    56.                 _friendNameField.text = string.Empty;
    57.             //}
    58.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Check the device logs. There's probably an exception being thrown, or else the code isn't even running. Put in Debug.Log() calls to prove you are actually calling it.

    If that does nothing then this might be some next things to consider:

    Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

    https://forum.unity.com/threads/using-unity-for-subscription-lists.1070663/#post-6910289

    https://forum.unity.com/threads/unity-web-request-acting-stupid.1096396/#post-7060150

    And setting up a proxy can be very helpful too, in order to compare traffic:

    https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity
     
  3. ayildiiz

    ayildiiz

    Joined:
    Feb 19, 2021
    Posts:
    5
    It seems like you're referring to an issue that came with the iOS update. I found a temporary solution for it by using the POST method and retrieving my data that way. Even though I didn't use a body in the GET method, it acted as if I did and blocked my API. Thank you for your attention and assistance.
     
    Kurt-Dekker likes this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Those two things do not reflect your claims and contradict each other ^^. Why do you create an upload handler? You must not use a request body when you do a get request. Some servers and clients don't care, may ignore or strip the body. However certain clients or servers may get upset when you try. The RFC specification has changed over the years and does not recommend using a GET body as at some point it was not allowed and certain implementations may already strip GET bodies, so the whole situation is unreliable and should be avoided.

    So remove that line where you create that upload handler. You don't need that "requestContent" either. You pass the name as a get parameter.

    HTTP is a text based protocol and a request to
    https://my.server.com/path/file?SomeVar=foobar

    would look something like this:

    Code (CSharp):
    1. GET /path/file?SomeVar=foobar HTTP/1.0
    2. Host: my.server.com
    3. Some-Other-Header: headerValue
    4.  
    5.  
    Note that the request header section is finished with an empty line. After that empty line the body would follow. So when you provide an upload handler with content, that content would be placed in the body. Like that:

    Code (CSharp):
    1. GET /path/file?SomeVar=foobar HTTP/1.0
    2. Host: my.server.com
    3. Content-Length: 6
    4. Content-Type: text/plain
    5. Some-Other-Header: headerValue
    6.  
    7. foobar
    That's what you're currently doing and that's what's the problem. Remove that body, it makes no sense unless you actually read the body on your server? Though in that case you should use a POST request which is expected to have a body. What that body looks like depends on your usecase. HTML form data for example is passed as an URL encoded string. So essentially quite similar to your search parameters that you appended to your URL.

    A POST request looks exactly the same, but the method at the very start would be POST instead of GET. That's all. It's just an interpretation thing by clients / servers and convention. So don't use a body with a GET request.