private IEnumerator UploadRequest(string url, string imgpath, UnityAction<string> callback)
{
WWWForm form = new WWWForm();
form.AddBinaryData("Image", File.ReadAllBytes(imgpath), Path.GetFileName(imgpath));
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
www.SetRequestHeader("Content-Type","application/octet-stream");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
callback?.Invoke(null);
}
else
{
callback?.Invoke(www.downloadHandler.text);
}
}
}
private IEnumerator SendMsgRequest(string url, string msg, UnityAction<string> callback)
{
using (UnityWebRequest www = UnityWebRequest.Post(url,UnityWebRequest.kHttpVerbPOST))
{
www.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(string.Format("{{ \"content\":\"{0}\" }}",msg)));
www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
callback?.Invoke(null);
}
else
{
callback?.Invoke(www.downloadHandler.text);
}
}
}
|