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 :
2、示例代码
using System;using System.Collections;using System.Collections.Generic;using UnityEngine;////// Get和Post最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。/// 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). */ ////// get方法 /// ///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); } } /// /// application/json连接类型 /// ///IEnumerator PostJson() { string fullUrl = @"http://xxxxxxxx"; //加入http 头信息 Dictionary JsonDic = new Dictionary (); 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); } } /// /// application/x-www-form-urlencoded连接类型 /// ///IEnumerator PostForm() { string fullUrl = @"http://xxxxxxxx"; //加入http 头信息 Dictionary JsonDic = new Dictionary (); 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;}