Unity3D之Get与Post

转载自:http://blog.csdn.net/cocos2der/article/details/38365645

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=565

unity3d中的www直接提供了web请求服务。使用也非常简单。

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections.Generic;  
  3. using System.Collections;  
  4.   
  5. public class WebManager : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start ()   
  9.     {  
  10.         // Request by get  
  11.         StartCoroutine(Get("http://www.cocos2dev.com/"));  
  12.   
  13.         // Request by post  
  14.         Dictionary<stringstring> dic = new Dictionary<stringstring> ();  
  15.         dic.Add("userId""6001345679887");  
  16.         dic.Add("eventId""10018");  
  17.         StartCoroutine(Post("http://192.168.1.102/api.php", dic));  
  18.     }  
  19.       
  20.     // Update is called once per frame  
  21.     void Update ()   
  22.     {  
  23.       
  24.     }  
  25.   
  26.     // Post  
  27.     IEnumerator Post(string url, Dictionary<stringstring>postData)   
  28.     {  
  29.         WWWForm form = new WWWForm();  
  30.         foreach(KeyValuePair<stringstring> postArg in postData)   
  31.         {  
  32.             form.AddField(postArg.Key, postArg.Value);  
  33.         }  
  34.           
  35.         WWW www = new WWW(url, form);  
  36.         yield return www;  
  37.           
  38.         if (www.error != null)   
  39.         {  
  40.             Debug.Log("error is :"+ www.error);  
  41.         }   
  42.         else  
  43.         {  
  44.             Debug.Log("request result :" + www.text);  
  45.         }  
  46.     }  
  47.       
  48.     // Get  
  49.     IEnumerator Get(string url)   
  50.     {  
  51.         WWW www = new WWW (url);  
  52.         yield return www;  
  53.           
  54.         if (www.error != null)   
  55.         {  
  56.             Debug.Log("error is :"+ www.error);  
  57.         }   
  58.         else   
  59.         {  
  60.             Debug.Log("request result :" + www.text);  
  61.         }  
  62.     }  
  63. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值