温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity WWW类Http Content-T 文本类型讲

发布时间:2020-02-26 06:14:16 来源:网络 阅读:244 作者:GuangYao_Li 栏目:游戏开发

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 : 需要在表单中进行文件上传时,就需要使用该格式

示例代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Get和Post最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。
/// </summary>
public class HttpTest : MonoBehaviour {

    /// <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;
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI