Untitled

 avatar
unknown
csharp
4 years ago
2.4 kB
5
Indexable
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine.UI;
using System.Text;

[Serializable]
public class MyClass
{
    public string user_email_address;
    public string user_password;
    //public string software_name_abbreviation;
    public object action_name;
}

public class FotoMasterAuthenticator : MonoBehaviour
{
    private string path;
    public Text systemMessage_reg;
    public Text systemMessage_login;
    public InputField userEmailLogin;
    public InputField passwordLogin;
    public InputField appIDLogin;

    public string url = "http://licensing.fotomasterltd.net/";

    public void StartCrossingLogin()
    {
        MyClass myObject = new MyClass();
        myObject.action_name = "user_login";
        myObject.user_email_address = userEmailLogin.text;
        myObject.user_password = passwordLogin.text;
        //myObject.software_name_abbreviation = appIDLogin.text;

        if (userEmailLogin.text != null && passwordLogin.text != null)
        { 
            string json = JsonUtility.ToJson(myObject);
            json = "data=" + json;
            File.WriteAllText(path, json);
            StartCoroutine(Upload(json));
            systemMessage_login.text = "Sending";
        }
        else
        {
            systemMessage_login.text = "One of the fields is not filled";
        }
    }

    public void Start()
    {
        path = Application.dataPath + "/StreamingAssets" + "/Form.txt";
    }

    IEnumerator Upload(string bodyJsonString)
    {
        var jsonBinary = System.Text.Encoding.UTF8.GetBytes(bodyJsonString);

        DownloadHandlerBuffer downloadHandlerBuffer = new DownloadHandlerBuffer();

        UploadHandlerRaw uploadHandlerRaw = new UploadHandlerRaw(jsonBinary);
        uploadHandlerRaw.contentType = "application/json";

        UnityWebRequest www = new UnityWebRequest(url, "POST", downloadHandlerBuffer, uploadHandlerRaw);

        yield return www.SendWebRequest();

        if (www.isNetworkError)
            Debug.LogError(string.Format("{0}: {1}", www.url, www.error));
        else
            Debug.Log(string.Format("Response: {0}", www.downloadHandler.text));
            systemMessage_login.text = "Sent!";


    }
}
Editor is loading...