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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM