Unity WWW类调用http
1、Http请求中Content-Type讲解
MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。
类型格式:type/subtype(;parameter)? type
主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
例如: Content-Type: text/html;charset:utf-8;
常见的媒体格式类型如下:
text/html : HTML格式 text/plain :纯文本格式 text/xml : XML格式 image/gif :gif图片格式 image/jpeg :jpg图片格式 image/png:png图片格式 application/xhtml+xml :XHTML格式 application/xml : XML数据格式 application/atom+xml :Atom XML聚合格式 application/json : JSON数据格式 application/pdf :pdf格式 application/msword : Word文档格式 application/octet-stream : 二进制流数据(如常见的文件下载) application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式) multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
2、示例代码
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// Get和Post最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。 /// </summary> public class HttpTest : MonoBehaviour { /* The WWW class can be used to send both GET and POST requests to the server. The WWW class will use GET by default and POST if you supply a postData parameter. See Also: WWWForm for a way to build valid form data for the postData parameter. Note: URLs passed to WWW class must be '%' escaped. Notes http://, https:// and file:// protocols are supported on iPhone. ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported. Note: When using file protocol on Windows and Windows Store Apps for accessing local files, you have to specify file:/// (with three slashes). */ /// <summary> /// get方法 /// </summary> /// <returns></returns> IEnumerator Get() { string fullUrl = @"http://xxxxxx?id=0";//id为参数 using (WWW www = new WWW(fullUrl)) { yield return www; if (www.error != null) { Debug.LogError("error:" + www.error); } Debug.Log(www.text); } } /// <summary> /// application/json连接类型 /// </summary> /// <returns></returns> IEnumerator PostJson() { string fullUrl = @"http://xxxxxxxx"; //加入http 头信息 Dictionary<string, string> JsonDic = new Dictionary<string, string>(); JsonDic.Add("Content-Type", "application/json"); //body SendData sendData = new SendData(); sendData.app_key = "1bf5376b82f88384ebe2297327ae2f9fe547dfed"; sendData.time_stamp= "1551174536"; sendData.nonce_str= "123456789"; sendData.sign= "1bf5376b82f88384ebe2297327ae2f9fe547dfed"; sendData.img_type= "URL"; byte[] data = System.Text.Encoding.Default.GetBytes(JsonUtility.ToJson(sendData)); using (WWW www = new WWW(fullUrl, data, JsonDic)) { yield return www; if (www.error != null) { Debug.LogError("error:" + www.error); } Debug.Log(www.text); } } /// <summary> /// application/x-www-form-urlencoded连接类型 /// </summary> /// <returns></returns> IEnumerator PostForm() { string fullUrl = @"http://xxxxxxxx"; //加入http 头信息 Dictionary<string, string> JsonDic = new Dictionary<string, string>(); JsonDic.Add("Content-Type", "application/x-www-form-urlencoded"); JsonDic.Add("cache-control", "no-cache"); JsonDic.Add("Postman-Token", "901451c5-e8c6-4322-8e63-754170323404"); //body WWWForm form = new WWWForm(); form.AddField("app_key", "1bf5376b82f88384ebe2297327ae2f9fe547dfed"); form.AddField("time_stamp", "1551174536"); form.AddField("nonce_str", "123456789"); form.AddField("sign", "1bf5376b82f88384ebe2297327ae2f9fe547dfed"); form.AddField("img_type", "URL"); using (WWW www = new WWW(fullUrl, form.data, JsonDic)) { yield return www; if (www.error != null) { Debug.LogError("error:" + www.error); } Debug.Log(www.text); } } } [Serializable] public class SendData { public string app_key; public string time_stamp; public string nonce_str; public string sign; public string img_type; }
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!
![](https://files-cdn.cnblogs.com/files/Jason-c/%E5%BE%AE%E4%BF%A1%E6%89%93%E8%B5%8F%E7%A0%81.bmp)