12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using BestHTTP;
- using Newtonsoft.Json;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- public class HttpGlobal : MonoBehaviour
- {
- public class LogBody {
- public string msg;
- public LogBody(string newMsg) {
- msg = newMsg;
- }
- }
- public static void SendLogToServer(string logContent) {
- logContent = string.Format("用户【{0}】", UserData.realName) + logContent;
- Debug.Log(HttpAddress.logAddress);
- BestHTTP.HTTPRequest request = new BestHTTP.HTTPRequest(new System.Uri(HttpAddress.logAddress), BestHTTP.HTTPMethods.Post, (HTTPRequest request1, HTTPResponse response) =>
- {
- try
- {
- Debug.Log(response.DataAsText);
- LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
- if (loginInfo.code == 200)
- {
- Debug.Log("发送日志成功,Content:" + logContent.ToString());
- }
- else {
- Debug.Log("发送日志失败,Reason:" + loginInfo.msg);
- }
- }
- catch (Exception e)
- {
- Debug.Log("发送日志失败,Reason:" + e.ToString());
- }
- });
- string jsonContent = JsonConvert.SerializeObject(new LogBody(logContent));
- request.RawData = Encoding.UTF8.GetBytes(jsonContent);
- request.AddHeader("token", UserData.token);
- request.AddHeader("Content-Type", "application/json");
- request.Send();
- }
- public static void SendPingToServer()
- {
- Debug.Log(HttpAddress.logAddress);
- BestHTTP.HTTPRequest request = new BestHTTP.HTTPRequest(new System.Uri(HttpAddress.pingAddress), BestHTTP.HTTPMethods.Post, (HTTPRequest request1, HTTPResponse response) =>
- {
- try
- {
- LoginInfoRet loginInfo = JsonConvert.DeserializeObject<LoginInfoRet>(response.DataAsText);
- if (loginInfo.code == 200)
- {
- Debug.Log("发送Ping成功");
- }
- else
- {
- Debug.Log("发送Ping失败,Reason:" + loginInfo.msg);
- }
- }
- catch (Exception e)
- {
- Debug.Log("发送Ping失败,Reason:" + e.ToString());
- }
- });
- request.AddHeader("token", UserData.token);
- request.AddHeader("Content-Type", "application/json");
- request.Send();
- }
- }
|