private string authorization;
private string jsonData = null;//在此案例中我假设json数据表为string 数值为null,实际上需要经过Json WriteValue的转译
void Start()
{
string url="http://192.168.xx.xxx/api/xxxxxxxxxxxxxxxx";//接口url
authorization="test,xiajibada";//与服务端约定的登录验证格式
StartCoroutine(Get(url,authorization, jsonData));
}
IEnumerator Get(string url, string auth, string json)
{
Dictionary<string,string> header = new Dictionary<string, string>();
byte[] postBytes;
header.Add("Content-Type", "application/json");//添加header
if(auth != null)
{
header.Add("Authorization",auth);
}
if(json != null)
{
//POST
//将数据转换成json数据流
postBytes = System.Text.Encoding.UTF8.GetBytes(json);
}
else
{
//GET
//此案例不需要json数据所以为null
postBytes=null;
}
WWW www = new WWW (url,postBytes,m_dir);
yield return www;
if (www.error != null)
{
Debug.Log("error is :"+ www.error);
}
else
{
Debug.Log("request result :" + www.text);
}
}