WWW网络请求
采用WWW获取网络数据:
(一)get
1)天气数据下载
private string weatherApi = "http://www.sojson.com/open/api/weather/json.shtml?city={0}";
//private string weatherApi = "http://www.sojson.com/open/api/weather/xml.shtml?city={0}"; //xml format
public string city = "闵行区";
private string fullWeatherApi;
public void OnRequest()
{
StartCoroutine(DataRequest(fullWeatherApi));
}
private bool isResponseValid(WWW www)
{
if (www.error != null)
{
print("Conn failed!");
return false;
}
if(string.IsNullOrEmpty(www.text))
{
print("Bad result");
return false;
}
return true;
}
private IEnumerator DataRequest(string url)
{
WWW www = new WWW(url);
yield return www;
if (!isResponseValid(www)) yield break;
print(www.text);
}
2)texture下载
public GameObject quad; private Texture2D downloadTexture; public void OnRequest() { ImageManage(SetSpirite); } private void ImageManage(Action<Texture2D> action) { if(downloadTexture ==null)//缓存检测 { StartCoroutine(ImageRequest(beautyApi, action)); } else { action(downloadTexture); } } private IEnumerator ImageRequest(string url,Action<Texture2D> action) { WWW www = new WWW(url); yield return www; print("request"); downloadTexture = www.texture; action(www.texture); } private void SetSpirite(Texture2D texture) { quad.GetComponent<MeshRenderer>().material.mainTexture = texture; }
(二)post
public string url = "https://api-cn.faceplusplus.com/facepp/v3/detect"; private Hashtable postData = new Hashtable(); public void OnRequest() { StartCoroutine(RequestData()); } IEnumerator RequestData() { WWWForm postForm = new WWWForm(); foreach(DictionaryEntry arg in postData) { postForm.AddField(arg.Key.ToString(), arg.Value.ToString()); } WWW www = new WWW(url, postForm); yield return www; print(www.text); } // Use this for initialization void Start () { postData.Add("api_key", "**************"); postData.Add("api_secret", "**************"); postData.Add("image_url", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508759666809&di=b3748df04c5e54d18fe52ee413f72f37&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F562c11dfa9ec8a1389c45414fd03918fa1ecc06c.jpg"); } // Update is called once per frame void Update () { }
补充:
post文件可通过WWWForm的AddBinaryData方法即可
第一个参数即为要post的文件的参数名称,第二参数为byte[]类型的文件,第三个为文件名称,第四个为类型。后两个参数可略掉。
form.AddBinaryData("image_file", image, name, "file/jpg");
public IEnumerator WwwPostRequest(byte[] image,string name,int i) { WWWForm form = new WWWForm(); form.AddField("api_key", "*************"); form.AddField("api_secret", "***************"); form.AddField("faceset_token", "***************"); form.AddBinaryData("image_file", image, name, "file/jpg"); WWW w = new WWW("**********************", form); yield return w; }
补充:
unity 通过www下载时,通过重载的形式来确定get,post方法(不同post方式也在重载中确定);
public WWW(string url, WWWForm form);//对应post的multipart/form-data方式
public WWW(string url, byte[] postData);//对应application/x-www-form-urlencoded(此方式对应postdata为把传入参数按&符号连接后转化为byte[])和application/json方式(postdata为即把传入参数转换成json格式,在转化为byte[])
[Obsolete("This overload is deprecated. Use UnityEngine.WWW.WWW(string, byte[], System.Collections.Generic.Dictionary<string, string>) instead.")]
public WWW(string url, byte[] postData, Hashtable headers);
public WWW(string url, byte[] postData, Dictionary<string, string> headers);//即通过Dictionary指定post的方式如下:
System.Collections.Hashtable headers = new System.Collections.Hashtable();
headers.Add("Content-Type", "application/x-www-form-urlencoded");//哈希表的数据格式
string data = "username=post&password=6789";
byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(data);
WWW www = new WWW("http://127.0.0.1/Test.php", bs, headers);