120 lines
3.5 KiB
C#
120 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using BF;
|
|
using System;
|
|
using System.Text;
|
|
using BestHTTP;
|
|
|
|
namespace BF
|
|
{
|
|
public class BIReport : ManagerBase
|
|
{
|
|
Uri Uri;
|
|
#region override
|
|
|
|
static BIReport instance;
|
|
public static BIReport Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new BIReport();
|
|
return instance;
|
|
}
|
|
|
|
BIReport() { }
|
|
public override void Init()
|
|
{
|
|
base.Init();
|
|
}
|
|
|
|
public override void Destroy()
|
|
{
|
|
base.Destroy();
|
|
instance = null;
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
base.Update();
|
|
}
|
|
|
|
public override void LateUpdate()
|
|
{
|
|
base.LateUpdate();
|
|
}
|
|
|
|
public override void SetMono(MonoBehaviour mono)
|
|
{
|
|
base.SetMono(mono);
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 设置一个默认URI
|
|
/// </summary>
|
|
public void SetUri(string uri)
|
|
{
|
|
Uri = new Uri(uri);
|
|
}
|
|
|
|
public bool HasUri()
|
|
{
|
|
if (Uri == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void Post(string data, long offset, Action<HTTPRequest, HTTPResponse> callback)
|
|
{
|
|
if (Uri == null)
|
|
{
|
|
return;
|
|
}
|
|
var time = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + offset;
|
|
var date = time.ToString();
|
|
|
|
var httpRequest = new HTTPRequest(Uri, HTTPMethods.Post, (req, httpResponse) =>
|
|
{
|
|
if (httpResponse != null && httpResponse.IsSuccess == true)
|
|
{
|
|
BFLog.Log("BI report get response, response status is " + httpResponse.StatusCode);
|
|
}
|
|
else
|
|
{
|
|
BFLog.Log("BI report no response");
|
|
}
|
|
callback?.Invoke(req, httpResponse);
|
|
});
|
|
// var authorization = Md5Encrypt.Encrypt("fbiubi9gewaga=niu1n3091mlnoahgawng" + date);
|
|
// httpRequest.SetHeader("Authorization", authorization);
|
|
httpRequest.SetHeader("Date", date);
|
|
httpRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
httpRequest.RawData = Encoding.UTF8.GetBytes(data);
|
|
httpRequest.Send();
|
|
}
|
|
|
|
public void PostWithURL(string url, string data, Action<HTTPRequest, HTTPResponse> callback)
|
|
{
|
|
|
|
var httpRequest = new HTTPRequest(new Uri(url), HTTPMethods.Post, (req, httpResponse) =>
|
|
{
|
|
callback?.Invoke(req, httpResponse);
|
|
});
|
|
httpRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
httpRequest.RawData = Encoding.UTF8.GetBytes(data);
|
|
httpRequest.Send();
|
|
}
|
|
|
|
public void SendRequest(string url, string data, Action<HTTPRequest, HTTPResponse> callback)
|
|
{
|
|
var httpRequest = new HTTPRequest(new Uri(url), HTTPMethods.Get, (req, httpResponse) =>
|
|
{
|
|
callback?.Invoke(req, httpResponse);
|
|
});
|
|
httpRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
httpRequest.RawData = Encoding.UTF8.GetBytes(data);
|
|
httpRequest.Send();
|
|
}
|
|
}
|
|
} |