Unity3D 网络通信_HTTP协议:获取网络图片、内容

发布时间:2020-06-23 21:19:09 作者:蓬莱仙羽
来源:网络 阅读:1071

自己写的测试demo,一个功能一个功能测试着做的,没有什么结构,凑合看吧。

http协议,在手机平台,URL必须必带http://头。

此脚本主要实现了 

更多常用WEBService:http://www.webxml.com.cn/zh_cn/web_services.aspx

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Xml;  
  5. using System.IO;  
  6.   
  7. public class HTTPDemo : MonoBehaviour  
  8. {  
  9.     public string HostName = "http://www.webxml.com.cn";  
  10.     //城市天气预报服务   
  11.     public string URLPath = "/WebServices/WeatherWebService.asmx/getWeatherbyCityName";  
  12.     //获得验证码服务(直接获得图片)  
  13.     private string PictureName = "/WebServices/ValidateCodeWebService.asmx/cnValidateImage?byString='Picture'";  
  14.     //获得验证码服务(获得图片字节流)  
  15.     private string PictureByteName = "/WebServices/ValidateCodeWebService.asmx/cnValidateByte?byString='picByte'";  
  16.           
  17.     private Texture2D mPicture;  
  18.     private Texture2D mPictureByte;  
  19.     private Texture2D mConvertPNG;  
  20.   
  21.     public string[] Parameters = new string[] { "theCityName" };  
  22.   
  23.     private string XMLContent = "null";  
  24.   
  25.     public string testC = "null";  
  26.   
  27.     void OnGUI()  
  28.     {  
  29.         //显示测试信息   
  30.         GUI.Label(new Rect(100, 10, 1000, 38), testC);  
  31.   
  32.   
  33.         //表单传值  
  34.         if (GUI.Button(new Rect(10, 50, 100, 60), "post"))  
  35.         {  
  36.             postWeatherbyCityName("北京");  
  37.         }  
  38.         GUI.Button(new Rect(120, 80, 100 + getJindu() * 100, 20), (getJindu() * 100) + "%");  
  39.   
  40.   
  41.         //get传值(android平台不支持中文参数)  
  42.         if (GUI.Button(new Rect(10, 130, 100, 60), "get"))  
  43.         {  
  44.             getWeatherbyCityName("58367");//上海  
  45.         }  
  46.         GUI.Button(new Rect(120, 150, 100 + getJindu() * 100, 20), (getJindu() * 100) + "%");  
  47.   
  48.   
  49.    
  50.         //显示读取到的天气预报原始信息(xml格式)  
  51.         GUI.Label(new Rect(10, 220, 380, 500), mContent);  
  52.   
  53.   
  54.   
  55.         //解析xml   
  56.         if (GUI.Button(new Rect(500, 200, 120, 60), "AnalysisXML"))  
  57.         {  
  58.             XMLContent = AnalysisXML();  
  59.         }  
  60.         GUI.Label(new Rect(410, 220, 380, 500), XMLContent);  
  61.   
  62.   
  63.   
  64.         //下载网络图片   
  65.         if (GUI.Button(new Rect(10, 750, 80, 60), "downPic"))  
  66.         {  
  67.             downloadPicture(PictureName);  
  68.         }  
  69.         GUI.Label(new Rect(100, 760, 200, 200), mPicture);  
  70.   
  71.   
  72.         //下载网络图片 (base64格式)  
  73.         if (GUI.Button(new Rect(350, 750, 80, 60), "downPicByte"))  
  74.         {  
  75.             downloadPictureByte(PictureByteName);  
  76.         }  
  77.         GUI.Label(new Rect(450, 760, 200, 200), mPictureByte);  
  78.     }  
  79.   
  80.     public void postWeatherbyCityName(string str)  
  81.     {  
  82.         //将参数集合封装到Dictionary集合方便传值  
  83.         Dictionary<stringstring> dic = new Dictionary<stringstring>();  
  84.   
  85.         //参数  
  86.         dic.Add(Parameters[0], str);  
  87.   
  88.         StartCoroutine(POST(HostName + URLPath , dic));  
  89.     }  
  90.   
  91.     public void getWeatherbyCityName(string str)  
  92.     {  
  93.         //将参数集合封装到Dictionary集合方便传值  
  94.         Dictionary<stringstring> dic = new Dictionary<stringstring>();  
  95.   
  96.         //参数  
  97.         dic.Add(Parameters[0], str);  
  98.   
  99.         StartCoroutine(GET(HostName + URLPath , dic));  
  100.     }  
  101.   
  102.     //下载图片   
  103.     public void downloadPicture(string picName)  
  104.     {  
  105.         testC ="picurl = " + picName;  
  106.   
  107.         StartCoroutine(GETTexture(HostName + picName));  
  108.     }  
  109.   
  110.     //下载图片(字节流)  
  111.     public void downloadPictureByte(string picName)  
  112.     {  
  113.         StartCoroutine(GETTextureByte(HostName + picName));  
  114.     }  
  115.   
  116.     /*----------------------------------------------------Helper----------------------------------------------------------------------------*/  
  117.   
  118.     private float mJindu = 0;  
  119.     private string mContent;  
  120.   
  121.     public float getJindu()  
  122.     {  
  123.         return mJindu;  
  124.     }  
  125.   
  126.     //POST请求(Form表单传值、效率低、安全 ,)  
  127.     IEnumerator POST(string url, Dictionary<stringstring> post)  
  128.     {  
  129.         //表单   
  130.         WWWForm form = new WWWForm();  
  131.         //从集合中取出所有参数,设置表单参数(AddField()).  
  132.         foreach (KeyValuePair<stringstring> post_arg in post)  
  133.         {  
  134.             form.AddField(post_arg.Key, post_arg.Value);  
  135.         }  
  136.         //表单传值,就是post   
  137.         WWW www = new WWW(url, form);  
  138.   
  139.         yield return www;  
  140.         mJindu = www.progress;  
  141.   
  142.         if (www.error != null)  
  143.         {  
  144.             //POST请求失败  
  145.             mContent =  "error :" + www.error;  
  146.         }  
  147.         else  
  148.         {  
  149.             //POST请求成功  
  150.             mContent = www.text;  
  151.         }  
  152.     }  
  153.   
  154.     //GET请求(url?传值、效率高、不安全 )  
  155.     IEnumerator GET(string url, Dictionary<stringstringget)  
  156.     {  
  157.         string Parameters;  
  158.         bool first;  
  159.         if (get.Count > 0)  
  160.         {  
  161.             first = true;  
  162.             Parameters = "?";  
  163.             //从集合中取出所有参数,设置表单参数(AddField()).  
  164.             foreach (KeyValuePair<stringstring> post_arg in get)  
  165.             {  
  166.                 if (first)  
  167.                     first = false;  
  168.                 else  
  169.                     Parameters += "&";  
  170.   
  171.                 Parameters += post_arg.Key + "=" + post_arg.Value;  
  172.             }  
  173.         }  
  174.         else  
  175.         {  
  176.             Parameters = "";  
  177.         }  
  178.   
  179.         testC ="getURL :" + Parameters;  
  180.   
  181.         //直接URL传值就是get  
  182.         WWW www = new WWW(url + Parameters);  
  183.         yield return www;  
  184.         mJindu = www.progress;  
  185.   
  186.         if (www.error != null)  
  187.         {  
  188.             //GET请求失败  
  189.             mContent = "error :" + www.error;  
  190.         }  
  191.         else  
  192.         {  
  193.             //GET请求成功  
  194.             mContent = www.text;  
  195.         }  
  196.     }  
  197.   
  198.     IEnumerator GETTexture(string picURL)  
  199.     {  
  200.         WWW wwwTexture = new WWW(picURL);  
  201.   
  202.         yield return wwwTexture;  
  203.   
  204.         if (wwwTexture.error != null)  
  205.         {  
  206.             //GET请求失败  
  207.             Debug.Log("error :" + wwwTexture.error);  
  208.         }  
  209.         else  
  210.         {  
  211.             //GET请求成功  
  212.             mPicture = wwwTexture.texture;  
  213.         }  
  214.     }  
  215.   
  216.     string PicByte;  
  217.     IEnumerator GETTextureByte(string picURL)  
  218.     {  
  219.         WWW www = new WWW(picURL);  
  220.   
  221.         yield return www;  
  222.   
  223.         if (www.error != null)  
  224.         {  
  225.             //GET请求失败  
  226.             Debug.Log("error :" + www.error);  
  227.         }  
  228.         else  
  229.         {  
  230.             //GET请求成功  
  231.             Debug.Log("PicBytes text = " + www.text);  
  232.   
  233.             XmlDocument xmlDoc = new XmlDocument();  
  234.             xmlDoc.Load(new StringReader(www.text));  
  235.   
  236.             //通过索引查找子节点   
  237.             PicByte = xmlDoc.GetElementsByTagName("base64Binary").Item(0).InnerText;  
  238.             testC = PicByte;  
  239.   
  240.             mPictureByte = BttetoPic(PicByte);  
  241.         }  
  242.     }  
  243.   
  244.     //解析XML   
  245.     string AnalysisXML()  
  246.     {  
  247.         string str = "";  
  248.   
  249.         XmlDocument xmlDoc = new XmlDocument();  
  250.         xmlDoc.Load(new StringReader(mContent));  
  251.   
  252.         //得到文档根节点的所有子节点集合   
  253.         //XmlNodeList nodes = xmlDoc.DocumentElement.ChildNodes;  
  254.         //通过节点名得到节点集合  
  255.         XmlNodeList nodes = xmlDoc.GetElementsByTagName("string");  
  256.   
  257.         //通过索引查找子节点   
  258.         str += "item[1] = " + xmlDoc.GetElementsByTagName("string").Item(1).InnerText + "\n\n";  
  259.   
  260.         //遍历所有子节点  
  261.         foreach (XmlElement element in nodes)  
  262.         {  
  263.             if (element.Name == "string")  
  264.             {  
  265.                 str += element.InnerText + "\n";  
  266.             }  
  267.         }  
  268.           
  269.         return str;  
  270.     }  
  271.   
  272.     //图片与byte[]互转  
  273.     public void convertPNG(Texture2D pic)  
  274.     {  
  275.         byte[] data = pic.EncodeToPNG();  
  276.         Debug.Log("data = " + data.Length + "|" + data[0]);  
  277.         mConvertPNG = new Texture2D(200, 200);  
  278.         mConvertPNG.LoadImage(data);  
  279.     }  
  280.   
  281.     //byte[]与base64互转   
  282.     Texture2D BttetoPic(string base64)  
  283.     {   
  284.         Texture2D pic = new Texture2D(200,200);  
  285.         //将base64转码为byte[]   
  286.         byte[] data = System.Convert.FromBase64String(base64);  
  287.         //加载byte[]图片  
  288.         pic.LoadImage(data);  
  289.   
  290.         string base64str = System.Convert.ToBase64String(data);  
  291.         Debug.Log("base64str = " + base64str);  
  292.   
  293.         return pic;  
  294.     }  
  295. }  
分享到: 
推荐阅读:
  1. 浏览器内核与Web App
  2. 将HTML5 Canvas的内容保存为图片

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

un

上一篇:怎么用java判断字符是否是日期

下一篇:完美解决eclipse安装Genymotion插件找不到图标问题

相关阅读

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

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