


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、unity3d网络通信自己写的测试demo ,一个功能一个功能测试着做的, 没有什么结构,凑合看吧。http协议,在手机平台,url必须必带http:/ 头。此脚本主要实现了分别用 pose和 get方式获取天气预报信息( xml格式)。解析 xml获取网络图片获取网络图片( base64格式)base64与 byte互转 byte 与 texture2d(图片)互转更多常用 webservice:using unityengine;using system.collections;using system.collections.generic;using system.xml;using
2、system.io;public class httpdemo : monobehaviourpublic string hostname = ""/ 城市天气预报服务public string urlpath ="/webservices/weatherwebservice.asmx/getweatherbycityname"/获得验证码服务(直接获得图片) private string picturename = "/webservices/validatecodewebservice.asmx/cnvalidateimage?bystri
3、ng='picture'"/获得验证码服务 (获得图片字节流)private string picturebytename = "/webservices/validatecodewebservice.asmx/cnvalidatebyte?bystring='picbyte'"private texture2d mpicture;private texture2d mpicturebyte;private texture2d mconvertpng; public string parameters = new string &q
4、uot;thecityname" ; private string xmlcontent = "null"public stringtestc = "null"void ongui()/ 显示测试信息gui.label(new rect(100, 10, 1000, 38), testc);/ 表单传值if (gui.button(new rect(10, 50, 100, 60), "post")postweatherbycityname("北京");gui.button(new rect(120, 8
5、0, 100 + getjindu() * 100, 20), (getjindu() * 100) + "%");/get传值( android平台不支持中文参数)if(gui.button(new rect(10, 130, 100, 60), "get")getweatherbycityname("58367");/上gui.button(new rect(120, 150,海100 + getjindu() * 100, 20), (getjindu() * 100) + "%");/显示读取到的天气预报原
6、始信息(xml格式)gui.label(new rect(10, 220, 380, 500), mcontent);/解析 xmlif (gui.button(new rect(500, 200, 120, 60), "analysisxml")xmlcontent = analysisxml(); gui.label(new rect(410, 220, 380, 500), xmlcontent);/下载网络图片if (gui.button(new rect(10,750, 80, 60), "downpic")downloadpicture(pi
7、cturename);gui.label(new rect(100, 760, 200, 200),mpicture);/ 下载网络图片( base64格式)if (gui.button(new rect(350, 750, 80, 60), "downpicbyte")downloadpicturebyte(picturebytename);gui.label(new rect(450, 760, 200, 200), mpicturebyte);public voidpostweatherbycityname(string str)/ 将参数集合封装到dictionar
8、y集合方便传值dictionary<string, string> dic = newdictionary<string, string>();/参数dic.add(parameters0, str);startcoroutine(post(hostname + urlpath , dic);public void getweatherbycityname(string str)/ 将参数集合封装到dictionary集合方便传值dictionary<string, string> dic = new dict
9、ionary<string, string>();/参数dic.add(parameters0, str);startcoroutine(get(hostname + urlpath , dic);/下载图片public void downloadpicture(string picname)testc ="picurl = " + picname;startcoroutine(gettexture(hostname + picname);/下载图片(字节流) public void downloadpicturebyte(string picn
10、ame)startcoroutine(gettexturebyte(hostname + picname);/*-helper-*/private float mjindu = 0;private string mcontent; public float getjindu()returnmjindu;/post请求 (form表单传值、 效率低、安全, )ienumerator post(string url, dictionary<string, string> post)/ 表wwwform form = new wwwform();单/从集合中取出所有参数,
11、设置表单参数(addfield().foreach (keyvaluepair<string, string> post_arg in post)form.addfield(post_arg.key, post_arg.value);/ 表单传值,就是postwww www = new www(url, form);yield return www;mjindu = gress;if (www.error != null)/post请求失败mcontent ="error :" +www.error;else/post请求成功mco
12、ntent = www.text;/get 请求( url? 传值、效率高、不安全)ienumerator get(string url, dictionary<string, string> get)string parameters;bool first;if (get.count > 0)first = true; parameters = "?"/ 从集合中取出所有参数, 设置表单参数(addfield().foreach (keyvaluepair<string, string> post_ar
13、g in get)if (first)first = false;elseparameters += "&"parameters+= post_arg.key + "=" +post_arg.value;elseparameters = ""testc ="geturl :" +parameters;/ 直接 url传值就是getwww www = new www(url + parameters);yield return www;mjindu = gress;if (www.err
14、or != null)/get请求失败mcontent = "error :" +www.error;else/get请求成功mcontent= www.text;ienumerator gettexture(string picurl)www wwwtexture = new www(picurl);yield return wwwtexture;if (wwwtexture.error != null)/get请求失败debug.log("error :" + wwwtexture.error);else/get请求成功mpicture = wwwt
15、exture.texture; string picbyte;ienumerator gettexturebyte(string picurl)www www = new www(picurl);yield return www;if (www.error != null)/get请debug.log("error :" +求失败www.error);else/get请求成功debug.log("picbytes text = " + www.text); xmldocument xmldoc = new xmldocument(); xmldoc.lo
16、ad(new stringreader(www.text);/通过索引查找子节点picbyte =xmldoc.getelementsbytagname("base64binary").item(0).innertext;testc = picbyte;mpicturebyte = bttetopic(picbyte);/解析 xmlstring analysisxml()string str = ""xmldocument xmldoc = new xmldocument();xmldoc.load(new stringreader(mcontent)
17、;/得到文档根节点的所有子节点集合/xmlnodelist nodes = xmldoc.documentelement.childnodes;/ 通过节点名得到节点集合xmlnodelist nodes =xmldoc.getelementsbytagname("string");/通过索引查找子节点str += "item1 = " + xmldoc.getelementsbytagname("string").item(1).innertext + "nn"/ 遍历所有子节点foreach (xmleleme
18、nt element in nodes)if (element.name = "string")str += element.innertext + "n"return str;/ 图片与 byte 互转public void convertpng(texture2d pic)byte data = pic.encodetopng();debug.log("data =" + data.length + "|" + data0);mconvertpng = new texture2d(200, 200);mconvertpng.loadimage(data);/byte与 base64互转tex
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电影票务平台地区级代理合同
- 合同法修订案:第一章 合同的订立与生效
- 外资制造业-员工培训合同范本
- 木材采购与销售合同模板
- 流动人口计划生育协作合同
- 干股收益分配合同(范本)
- 企事业单位监控布防合同模板
- 合同责任死亡赔偿金额解析
- 企业合作合同(担保互惠)
- 学校食堂食材采购合同模板
- Unit5 What day is it today?(教学设计)-2023-2024学年教科版(广州)英语四年级下册
- 《住院患者身体约束的护理》团体标准解读课件
- 2024年黑龙江建筑职业技术学院单招职业适应性测试题库全面
- 硅酸盐水泥熟料矿物组成及配料计算概述(共101页).ppt
- 急诊与灾难医学第二版配套课件 02 急性发热
- 部编版四年级道德与法治下册4《买东西的学问》第1课时课件
- 外研版英语(新标准)八年级下册教案(全册)
- 教师听课评分表
- 项目章程模板范文
- 泰山产业领军人才工程系统
- 轮扣架支模体系材料量计算
评论
0/150
提交评论