RuralPowerCompetition_yizhe.../RuralPowerCompetition_yizheng1/Assets/Temp/Test_0106/PostTest.cs

59 lines
1.8 KiB
C#

using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PostTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
[ContextMenu("post")]
private void Post()
{
string url = "";
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
//keyValuePairs.Add("http://172.16.1.26:5000/api/AddJianktx", "");
//keyValuePairs.Add();
//keyValuePairs.Add();
//keyValuePairs.Add();
StartCoroutine(Post(url,keyValuePairs,(a,s)=> {
}));
}
public static IEnumerator Post(string url, Dictionary<string, string> keyValuePairs, System.Action<bool, string> action)
{
WWWForm form = new WWWForm();
foreach (var item in keyValuePairs)
{
form.AddField(item.Key, item.Value);
}
UnityWebRequest webRequest = UnityWebRequest.Post(url, form);
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log(webRequest.error);
action.Invoke(false, string.Empty);
}
else
{
string msg = webRequest.downloadHandler.text;
Debug.Log(msg);
JObject JO = JObject.Parse(msg);
if (JO["state"].ToString().Equals("true") || JO["state"].ToString().Equals("True"))
{
action.Invoke(true, JO["data"].ToString());
}
else
{
action.Invoke(false, string.Empty);
}
}
}
}