0

I'm facing a frustrating problem right now and I can't see any reason why this doesn't work since is pure Unity developer copy paste code.

I can't send data from Unity to website. Every Unity sent parameter is taken as empty.

That's how my [Web.cs] looks like:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;

public class Web : MonoBehaviour
{
    public IEnumerator Login(string Username, string Password)
    {
        Debug.Log(Username);
        Debug.Log(Password);

        WWWForm Form = new WWWForm();
        Form.AddField("loginUser", Username);
        Form.AddField("loginPass", Password);

        UnityWebRequest www = UnityWebRequest.Post("http://localhost/Login.php", Form);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
            Debug.Log(www.error);
        else
        {
            Debug.Log("Form upload complete!");
            Debug.Log(www.downloadHandler.text);
        }
    }
}

And I'm calling it via Login.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Login : MonoBehaviour
{
    public InputField UserName;
    public InputField Password;
    public Button myButton;

    private void Start()
    {
        myButton.onClick.AddListener(() =>
        {
            StartCoroutine(Main.Instance.Web.Login(UserName.text, Password.text));
        });
    }

}

The php code looks like:

<?php
    $UserName = $_GET["loginUser"];
    $Password = $_GET["loginPass"];
    
    $myfile = fopen("testfile.txt", "a");
    fwrite($myfile, "Username: $UserName\nPassowrd: $Password\n\n");
?>

So, when I manually acces the path link, I get the expected results:

http://localhost/Login.php?loginUser=Test&loginPass=Ok

https://i.imgur.com/s0bP5tJ.png

But when I do it with Unity, XAMPP writes the parameters as empty.

A debug photo:

https://i.imgur.com/r1IUTy2.png

2 Answers2

2

It appears you're sending a POST request to a GET request.

Try this: UnityWebRequest.Get(...)

Kale_Surfer_Dude
  • 784
  • 3
  • 10
0

@Kale, thank you, I kinda still don't understand what's the difference between post and get methods :-?

For future searchers, that's how I managed to resolve the issue:

public class Web : MonoBehaviour
{
    public IEnumerator Login(string Username, string Password)
    {
        WWWForm Form = new WWWForm();
        string Link = "http://localhost/Login.php?loginUser=" + Username + "&loginPass=" + Password;

        UnityWebRequest www = UnityWebRequest.Get(Link);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
            Debug.Log(www.error);
        else
        {
            Debug.Log("Form upload complete!");
            Debug.Log(www.downloadHandler.text);
        }
    }
}
  • Basically, a GET request sends information via URL - better for navigation, and post sends information more or less "behind the scenes" - better for login info. More info: https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – Kale_Surfer_Dude Sep 20 '20 at 20:55