Unity3D 网络通信_HTTP协议:获取网络图片、内容
自己写的测试demo,一个功能一个功能测试着做的,没有什么结构,凑合看吧。
http协议,在手机平台,URL必须必带http://头。
此脚本主要实现了
分别用pose和get方式获取天气预报信息(XML格式)。解析XML
获取网络图片获取网络图片(base64格式)base64与byte[]互转byte[]与Texture2D(图片)互转
更多常用WEBService:http://www.webxml.com.cn/zh_cn/web_services.aspx
usingUnityEngine; usingSystem.Collections; usingSystem.Collections.Generic; usingSystem.Xml; usingSystem.IO; publicclassHTTPDemo:MonoBehaviour { publicstringHostName="http://www.webxml.com.cn"; //城市天气预报服务 publicstringURLPath="/WebServices/WeatherWebService.asmx/getWeatherbyCityName"; //获得验证码服务(直接获得图片) privatestringPictureName="/WebServices/ValidateCodeWebService.asmx/cnValidateImage?byString='Picture'"; //获得验证码服务(获得图片字节流) privatestringPictureByteName="/WebServices/ValidateCodeWebService.asmx/cnValidateByte?byString='picByte'"; privateTexture2DmPicture; privateTexture2DmPictureByte; privateTexture2DmConvertPNG; publicstring[]Parameters=newstring[]{"theCityName"}; privatestringXMLContent="null"; publicstringtestC="null"; voidOnGUI() { //显示测试信息 GUI.Label(newRect(100,10,1000,38),testC); //表单传值 if(GUI.Button(newRect(10,50,100,60),"post")) { postWeatherbyCityName("北京"); } GUI.Button(newRect(120,80,100+getJindu()*100,20),(getJindu()*100)+"%"); //get传值(android平台不支持中文参数) if(GUI.Button(newRect(10,130,100,60),"get")) { getWeatherbyCityName("58367");//上海 } GUI.Button(newRect(120,150,100+getJindu()*100,20),(getJindu()*100)+"%"); //显示读取到的天气预报原始信息(xml格式) GUI.Label(newRect(10,220,380,500),mContent); //解析xml if(GUI.Button(newRect(500,200,120,60),"AnalysisXML")) { XMLContent=AnalysisXML(); } GUI.Label(newRect(410,220,380,500),XMLContent); //下载网络图片 if(GUI.Button(newRect(10,750,80,60),"downPic")) { downloadPicture(PictureName); } GUI.Label(newRect(100,760,200,200),mPicture); //下载网络图片(base64格式) if(GUI.Button(newRect(350,750,80,60),"downPicByte")) { downloadPictureByte(PictureByteName); } GUI.Label(newRect(450,760,200,200),mPictureByte); } publicvoidpostWeatherbyCityName(stringstr) { //将参数集合封装到Dictionary集合方便传值 Dictionary<string,string>dic=newDictionary<string,string>(); //参数 dic.Add(Parameters[0],str); StartCoroutine(POST(HostName+URLPath,dic)); } publicvoidgetWeatherbyCityName(stringstr) { //将参数集合封装到Dictionary集合方便传值 Dictionary<string,string>dic=newDictionary<string,string>(); //参数 dic.Add(Parameters[0],str); StartCoroutine(GET(HostName+URLPath,dic)); } //下载图片 publicvoiddownloadPicture(stringpicName) { testC="picurl="+picName; StartCoroutine(GETTexture(HostName+picName)); } //下载图片(字节流) publicvoiddownloadPictureByte(stringpicName) { StartCoroutine(GETTextureByte(HostName+picName)); } /*----------------------------------------------------Helper----------------------------------------------------------------------------*/ privatefloatmJindu=0; privatestringmContent; publicfloatgetJindu() { returnmJindu; } //POST请求(Form表单传值、效率低、安全,) IEnumeratorPOST(stringurl,Dictionary<string,string>post) { //表单 WWWFormform=newWWWForm(); //从集合中取出所有参数,设置表单参数(AddField()). foreach(KeyValuePair<string,string>post_arginpost) { form.AddField(post_arg.Key,post_arg.Value); } //表单传值,就是post WWWwww=newWWW(url,form); yieldreturnwww; mJindu=www.progress; if(www.error!=null) { //POST请求失败 mContent="error:"+www.error; } else { //POST请求成功 mContent=www.text; } } //GET请求(url?传值、效率高、不安全) IEnumeratorGET(stringurl,Dictionary<string,string>get) { stringParameters; boolfirst; if(get.Count>0) { first=true; Parameters="?"; //从集合中取出所有参数,设置表单参数(AddField()). foreach(KeyValuePair<string,string>post_arginget) { if(first) first=false; else Parameters+="&"; Parameters+=post_arg.Key+"="+post_arg.Value; } } else { Parameters=""; } testC="getURL:"+Parameters; //直接URL传值就是get WWWwww=newWWW(url+Parameters); yieldreturnwww; mJindu=www.progress; if(www.error!=null) { //GET请求失败 mContent="error:"+www.error; } else { //GET请求成功 mContent=www.text; } } IEnumeratorGETTexture(stringpicURL) { WWWwwwTexture=newWWW(picURL); yieldreturnwwwTexture; if(wwwTexture.error!=null) { //GET请求失败 Debug.Log("error:"+wwwTexture.error); } else { //GET请求成功 mPicture=wwwTexture.texture; } } stringPicByte; IEnumeratorGETTextureByte(stringpicURL) { WWWwww=newWWW(picURL); yieldreturnwww; if(www.error!=null) { //GET请求失败 Debug.Log("error:"+www.error); } else { //GET请求成功 Debug.Log("PicBytestext="+www.text); XmlDocumentxmlDoc=newXmlDocument(); xmlDoc.Load(newStringReader(www.text)); //通过索引查找子节点 PicByte=xmlDoc.GetElementsByTagName("base64Binary").Item(0).InnerText; testC=PicByte; mPictureByte=BttetoPic(PicByte); } } //解析XML stringAnalysisXML() { stringstr=""; XmlDocumentxmlDoc=newXmlDocument(); xmlDoc.Load(newStringReader(mContent)); //得到文档根节点的所有子节点集合 //XmlNodeListnodes=xmlDoc.DocumentElement.ChildNodes; //通过节点名得到节点集合 XmlNodeListnodes=xmlDoc.GetElementsByTagName("string"); //通过索引查找子节点 str+="item[1]="+xmlDoc.GetElementsByTagName("string").Item(1).InnerText+"\n\n"; //遍历所有子节点 foreach(XmlElementelementinnodes) { if(element.Name=="string") { str+=element.InnerText+"\n"; } } returnstr; } //图片与byte[]互转 publicvoidconvertPNG(Texture2Dpic) { byte[]data=pic.EncodeToPNG(); Debug.Log("data="+data.Length+"|"+data[0]); mConvertPNG=newTexture2D(200,200); mConvertPNG.LoadImage(data); } //byte[]与base64互转 Texture2DBttetoPic(stringbase64) { Texture2Dpic=newTexture2D(200,200); //将base64转码为byte[] byte[]data=System.Convert.FromBase64String(base64); //加载byte[]图片 pic.LoadImage(data); stringbase64str=System.Convert.ToBase64String(data); Debug.Log("base64str="+base64str); returnpic; } } 分享到:声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。