83 lines
2.4 KiB
C#
83 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Newtonsoft.Json;
|
|
using ProtoBuf;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using ZGame;
|
|
|
|
namespace Game
|
|
{
|
|
public interface IHttpManager
|
|
{
|
|
IEnumerator LoginAsync(string username, string password, ResponseData responseData);
|
|
}
|
|
|
|
public class HttpManager : IHttpManager
|
|
{
|
|
public IEnumerator LoginAsync(string username, string password, ResponseData responseData)
|
|
{
|
|
WWWForm wwwForm = new WWWForm();
|
|
wwwForm.AddField("username", username);
|
|
wwwForm.AddField("password", password);
|
|
|
|
// 序列化
|
|
var c2SLogin = new C2SLogin()
|
|
{
|
|
};
|
|
// using (var file = File.Create("example.bin"))
|
|
// {
|
|
// Serializer.Serialize(file, c2SLogin);
|
|
// }
|
|
|
|
using (UnityWebRequest request = UnityWebRequest.Post(NetworkConst.loginUrl, wwwForm))
|
|
{
|
|
yield return request.SendWebRequest();
|
|
|
|
// 处理响应
|
|
if (request.result == UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.Log("POST request successful!");
|
|
var downloadHandlerText = request.downloadHandler.text;
|
|
Debug.Log("Response: " + downloadHandlerText);
|
|
|
|
// var data = JsonConvert.DeserializeObject<ResponseData>(downloadHandlerText);
|
|
|
|
S2CLogin s2CLogin;
|
|
using (var file = File.Create("example.bin"))
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(downloadHandlerText);
|
|
file.Write(bytes);
|
|
s2CLogin = Serializer.Deserialize<S2CLogin>(file);
|
|
}
|
|
|
|
|
|
// if (data.code == "0")
|
|
// {
|
|
// responseData = data;
|
|
// }
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("POST request failed: " + request.error);
|
|
}
|
|
|
|
responseData = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class UserLoginData
|
|
{
|
|
public string username;
|
|
public string password;
|
|
}
|
|
|
|
public class ResponseData
|
|
{
|
|
public string code;
|
|
public string msg;
|
|
public object data;
|
|
}
|
|
} |