WXMC/.svn/pristine/2f/2ff19728fac22843b6c0121091379d9d7d30946a.svn-base

61 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2024-12-04 16:18:46 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetWorkManager : MonoSingletion<NetWorkManager>
{
private string ip = "http://114.132.68.192:8881";
public void Login(string username, string password) {
StartCoroutine(SendLogin(username, password));
}
private IEnumerator SendLogin(string username, string password)
{
string url = ip + "/api/user/login";
LoginData data = new LoginData();
data.username = username;
data.password = password;
string json = JsonUtility.ToJson(data);
// ת<><D7AA>JSON<4F><4E><EFBFBD>ݵ<EFBFBD><DDB5>ֽ<EFBFBD>
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
// <20><><EFBFBD><EFBFBD>UnityWebRequest<73><74><EFBFBD><EFBFBD>
UnityWebRequest www = new UnityWebRequest(url, "POST");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
www.SetRequestHeader("Content-Type", "application/json");
// <20><><EFBFBD><EFBFBD>POST<53><54><EFBFBD><EFBFBD>
www.uploadHandler = new UploadHandlerRaw(jsonToSend);
www.downloadHandler = new DownloadHandlerBuffer();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󲢵ȴ<F3B2A2B5><C8B4><EFBFBD>Ӧ
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Login successful! Response: " + www.downloadHandler.text);
}
}
}
[System.Serializable]
public class LoginData {
public string username;
public string password;
}