Unity Get和Post方法

来源:互联网 发布:js对象定义 编辑:程序博客网 时间:2024/06/06 01:54
IEnumerator Get(string url, Dictionary<string, string> getData) {        string param = null;        //生成参数        if (getData != null) {            param = "?";            int count = getData.Count;            int index = 0;            foreach (KeyValuePair<string, string> kvp in getData) {                if (index < count - 1) {                    param += kvp.Key + "=" + kvp.Value + "&";                }                else {                    param += kvp.Key + "=" + kvp.Value;                }                index++;            }        }        //将参数转换成url编码        param = WWW.EscapeURL(param);        WWW www = new WWW(url + param);        yield return www;        if (!string.IsNullOrEmpty(www.error)) {            Debug.Log(www.error);        } else {            Debug.Log(www.text);        }    }
IEnumerator Post(string url, Dictionary<string, string> postData) {        WWWForm wwwForm = new WWWForm();        foreach (KeyValuePair<string, string> kvp in postData) {            wwwForm.AddField(kvp.Key, kvp.Value);        }        WWW www = new WWW(saveUrl, wwwForm);        yield return www;        if (!string.IsNullOrEmpty(www.error)) {            Debug.Log(www.error);        }        else {            Debug.Log(www.text);        }    }
0 0
原创粉丝点击