Unity3D Http网络通讯协议 带header头部的GET与POST方法:

private string authorization;
private string jsonData = null;//在此案例中我假设json数据表为string 数值为null,实际上需要经过Json WriteValue的转译

void Start()
{
	string url="http://192.168.xx.xxx/api/xxxxxxxxxxxxxxxx";//接口url
	authorization="test,xiajibada";//与服务端约定的登录验证格式
	StartCoroutine(Get(url,authorization, jsonData));
}


IEnumerator Get(string url, string auth, string json) 
{
	Dictionary<string,string> header = new Dictionary<string, string>();
    byte[] postBytes;

    header.Add("Content-Type", "application/json");//添加header
    if(auth != null)
    {
   		header.Add("Authorization",auth);
    }
    
    if(json != null)
    {
        //POST
	    //将数据转换成json数据流
	    postBytes = System.Text.Encoding.UTF8.GetBytes(json);
	}
	else
	{
        //GET
		//此案例不需要json数据所以为null
		postBytes=null;
	}

    WWW www = new WWW (url,postBytes,m_dir);
    yield return www;

    if (www.error != null) 
    {
        Debug.Log("error is :"+ www.error);
    } 
    else 
    {
        Debug.Log("request result :" + www.text);
    }

}

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值