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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question UnityWebRequest returns empty string

Discussion in 'Multiplayer' started by ZaphodBeeblebrox21, Aug 27, 2020.

  1. ZaphodBeeblebrox21

    ZaphodBeeblebrox21

    Joined:
    Apr 17, 2020
    Posts:
    3
    Version: 2019.3.11f1

    I tried to add a simple online leaderboard to my game. I created a SQL database and wrote a PHP script which accesses it. So far so good.

    The PHP script's URL is the following:
    https://starcaders.com/leaderboard.php

    As you can see, the PHP script is working and displays the table's entries (only two test entries atm). My goal is to read the text on the page and save it as a string in Unity.

    Unfortunately, the code below returns an empty string.
    The isDone bool is also false.

    I also tried to add a "SendWebRequest()" after yield return leaderboard, but that leads to following error message: Curl error 51: Cert verify failed: UNITYTLS_X509VERIFY_FLAG_EXPIRED
    and also returns "hahaha TEST 123", my placeholder string.

    I also tried to retrieve text from other sites, but that returns an empty string as well.
    At this point, I am basically completely at a loss, which is why I created this thread.

    Thank you for your time, below you can see my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. using UnityEngine.Networking; // for UnityWebRequest
    6.  
    7. public class Leaderboard : MonoBehaviour
    8. {
    9.     void Start()
    10.     {
    11.         StartCoroutine("RequestLeaderboard");
    12.     }
    13.    
    14.     IEnumerator RequestLeaderboard()
    15.     {
    16.         UnityWebRequest leaderboard = UnityWebRequest.Get("http://starcaders.com/leaderboard.php"); //sends http request --> php script reads sql database
    17.         yield return leaderboard; //waits until request is done
    18.         string leaderboardString = "hahaha TEST 123";
    19.         if(!leaderboard.isNetworkError && !leaderboard.isHttpError)
    20.         {
    21.             leaderboardString = leaderboard.downloadHandler.text;
    22.         }
    23.  
    24.         Debug.Log(leaderboardString);
    25.     }
    26. }
     
  2. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    181
    You're not actually calling the request. Change line 17 to:
    Code (CSharp):
    1. yield return request.SendWebRequest();
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,465
    As already mentioned, you have to do SendWebRequest(), otherwise you are not sending request to server.
    And the error above is when using HTTPS instead of HTTP. If I try your URL in browser, I can see the same behavior: http works, for https browsers report security issues.