Unity的WWW是一个比较老的使用HTTP协议的API了,但是用法简单直观。
1.HTTP大体概念
(1) HTTP翻译名为——超文本传输协议。
(2)它的设计目的是保证客户机与服务器之间的通信。
(2)HTTP 的工作方式是客户机与服务器之间的请求-应答协议。模式是一问一答。
2.HTTP的两种请求方法
这两种方法在WWW中也对应着两种方法
(1)GET方法:从指定的资源请求数据(最好不要用GET方法请求重要数据)
(2)POST方法:向指定的资源提交数据
关于Psot和Get区别可参考链接:https://blog.csdn.net/ever_siyan/article/details/87935455
(1)WWW的GET方法
可以在协程中使用
//测试Get方法
StartCoroutine(getFunc("http://kun.show.ghostry.cn/?int=5"))
//通过这个URL来获取我们想要的数据或者是资源
IEnumerator getFunc(string url)
{
WWW getData = new WWW(url)
yield return getData;
if(getData.error != null)
{
Debug.Log(getData.error);
}
else
{
//在WWW中还有其他的变量比如 getData.texture, getData.bytes 可以自行跳转到WWW的定义查看
Debug.Log(getData.text);
}
}
(2)WWW的POST方法
//测试Post方法
WWWForm form = new WWWForm();
//也可以form .AddBinaryData("文件名",二进制byte数组)
//用AddBinaryData可以传一些较大的文件。
form.AddField("int", "6");
StartCoroutine(SendPost("http://kun.show.ghostry.cn/", form));
IEnumerator SendPost(string _url, WWWForm _wForm)
{
WWW postData = new WWW(_url, _wForm);
yield return postData;
if (postData.error != null)
{
Debug.Log(postData.error);
}
else
{
Debug.Log(postData.text);
}
}
(3)为他们添加表头用来验证
1、get加表头
IEnumerator getFunc(string url) //通过这个URL来获取我们想要的数据或者是资源
{
Dictionary<string,string> headers = new Dictionary<string,string>();
headers.add("Content-Type", "application/x-wwww-from-urlencoded");
WWW getData = New WWW(url,null,headers);//在中间加一个null即可带表头
yield return getData;
if(getData.error != null)
{
Debug.Log(getData.error);
}
else
{
Debug.Log(getData.text);
}
}
2、Post加表头
IEnumerator SendPost(string _url)
{
WWWForm _wForm = new WWWForm();
_wForm.AddField("uid", "123");//添加表单(验证是用防止恶意访问)
_wForm.AddBinaryData("file",System.Text.Encoding.UTF8.GetBytes(“66666666666”));
WWW postData = new WWW(_url, _wForm);
yield return postData;
if (postData.error != null)
{
Debug.Log(postData.error);
}
else
{
Debug.Log(postData.text);
}
}
3、使用JSON格式的Post请求
例如:接入百度AI文本纠错API,要求使用JSON格式的结构体来描述一个请求的具体内容。
接入请求说明如下:
代码如下:
private class Body
{
public string text = "";
}
IEnumerator ErrorCorrection()
{
string apiKey = "自己的apiKey";
string appSecret = "自己的appSecret";
WWW www = new WWW("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + appSecret);
yield return www;
if (www.isDone)
{
if (www.error == null)
{
JsonData res = JsonMapper.ToObject(www.text);
if (res["access_token"] != null)
{
access_token = res["access_token"].ToString();
Debug.Log(access_token);
//请求参数body
Body body = new Body();
body.text = "百度是一家人工只能公司";
string requestdata = JsonMapper.ToJson(body);
//不能这样:
//string requestdata = "text=百度是一家人工只能公司";
byte[] postBytes = Encoding.UTF8.GetBytes(requestdata);
//header
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("Content-Type", "application/json");
www = new WWW("https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet?charset=UTF-8&access_token=" + access_token, postBytes, header);
yield return www;
if (www.isDone)
{
if (www.error == null)
{
Debug.Log(www.text);
}
}
else
{
Debug.Log(www.error);
}
}
}
else
{
Debug.Log(www.error);
}
}
}
请求结果如下:
{
"log_id":4559397244907480534,
"item":{
"vec_fragment":[
{
"ori_frag":"只能",
"begin_pos":21,
"correct_frag":"智能",
"end_pos":27
}],
"score":0.961834,
"correct_query":"百度是一家人工智能公司"
},
"text":"百度是一家人工只能公司"
}