136 lines
4.9 KiB
C#
136 lines
4.9 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
namespace BF
|
|
{
|
|
public enum EDownloadCode
|
|
{
|
|
SUCCESS = 1,
|
|
ERROR,
|
|
}
|
|
|
|
public class BFWebRequestManager : ManagerBase
|
|
{
|
|
static BFWebRequestManager instance;
|
|
|
|
BFWebRequestManager() { }
|
|
|
|
public static BFWebRequestManager Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new BFWebRequestManager();
|
|
return instance;
|
|
}
|
|
|
|
public override void SetMono(MonoBehaviour mono)
|
|
{
|
|
base.SetMono(mono);
|
|
}
|
|
|
|
public override void Init()
|
|
{
|
|
}
|
|
|
|
public UnityWebRequest CreateWebRequest(string url, string method, int timeout)
|
|
{
|
|
var uniWebRequest = new UnityWebRequest(url, method, new DownloadHandlerBuffer(), null);
|
|
uniWebRequest.timeout = timeout;
|
|
return uniWebRequest;
|
|
}
|
|
|
|
public void Get(string url, Action<EDownloadCode, UnityWebRequest> callback, int timeout = 0)
|
|
{
|
|
var uniWebRequest = CreateWebRequest(url, UnityWebRequest.kHttpVerbGET, timeout);
|
|
var webRequest = uniWebRequest.SendWebRequest();
|
|
webRequest.completed += (operation) =>
|
|
{
|
|
GetRequestComplete(uniWebRequest, callback);
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// post data to remote
|
|
/// </summary>
|
|
/// <param name="fields">fieldname1|fieldvalue1|fieldname2|fieldvalue2|...</param>
|
|
public void Post(string url, string fields, Action<EDownloadCode, UnityWebRequest> callback, int timeout = 0)
|
|
{
|
|
var uniWebRequest = CreateWebRequest(url, UnityWebRequest.kHttpVerbPOST, timeout);
|
|
uniWebRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(fields));
|
|
uniWebRequest.uploadHandler.contentType = "application/json";
|
|
uniWebRequest.timeout = timeout;
|
|
var webRequest = uniWebRequest.SendWebRequest();
|
|
webRequest.completed += (operation) =>
|
|
{
|
|
PostRequestComplete(uniWebRequest, callback);
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// post data to remote
|
|
/// </summary>
|
|
/// <param name="fields">fieldname1|fieldvalue1|fieldname2|fieldvalue2|...</param>
|
|
public void PostForm(string url, string fields, Action<EDownloadCode, UnityWebRequest> callback, int timeout = 0)
|
|
{
|
|
var wwwForm = new WWWForm();
|
|
if (!string.IsNullOrEmpty(fields))
|
|
{
|
|
var fieldArray = fields.Split('|');
|
|
var fieldLen = fieldArray.Length;
|
|
BFLog.LogAssert(fieldLen % 2 == 0, "fields format error!");
|
|
|
|
for (int i = 0; i < fieldLen - 1; i += 2)
|
|
wwwForm.AddField(fieldArray[i], fieldArray[i + 1]);
|
|
}
|
|
|
|
var uniWebRequest = UnityWebRequest.Post(url, wwwForm);
|
|
var webRequest = uniWebRequest.SendWebRequest();
|
|
uniWebRequest.timeout = timeout;
|
|
webRequest.completed += (operation) =>
|
|
{
|
|
PostRequestComplete(uniWebRequest, callback);
|
|
};
|
|
}
|
|
|
|
void GetRequestComplete(UnityWebRequest uniWebRequest, Action<EDownloadCode, UnityWebRequest> callback)
|
|
{
|
|
if(uniWebRequest.result == UnityWebRequest.Result.ProtocolError || uniWebRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
{
|
|
Debug.LogWarning(uniWebRequest.error);
|
|
callback(EDownloadCode.ERROR, uniWebRequest);
|
|
}
|
|
else if (uniWebRequest.isDone && uniWebRequest.responseCode >= 200 && uniWebRequest.responseCode < 300)
|
|
{
|
|
callback(EDownloadCode.SUCCESS, uniWebRequest);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning(uniWebRequest.responseCode);
|
|
callback(EDownloadCode.ERROR, uniWebRequest);
|
|
}
|
|
uniWebRequest.Dispose();
|
|
}
|
|
|
|
void PostRequestComplete(UnityWebRequest uniWebRequest, Action<EDownloadCode, UnityWebRequest> callback)
|
|
{
|
|
if(uniWebRequest.result == UnityWebRequest.Result.ProtocolError || uniWebRequest.result == UnityWebRequest.Result.ConnectionError)
|
|
{
|
|
Debug.LogWarning(uniWebRequest.error);
|
|
callback(EDownloadCode.ERROR, uniWebRequest);
|
|
}
|
|
else if (uniWebRequest.isDone && uniWebRequest.responseCode >= 200 && uniWebRequest.responseCode < 300)
|
|
{
|
|
callback(EDownloadCode.SUCCESS, uniWebRequest);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning(uniWebRequest.responseCode);
|
|
callback(EDownloadCode.ERROR, uniWebRequest);
|
|
}
|
|
uniWebRequest.Dispose();
|
|
}
|
|
}
|
|
} |