自己写的测试demo,一个功能一个功能测试着做的,没有什么结构,凑合看吧。
http协议,在手机平台,URL必须必带http://头。
此脚本主要实现了
- 分别用pose和get方式获取天气预报信息(XML格式)。
- 解析XML
- 获取网络图片
- 获取网络图片(base64格式)
- base64与byte[]互转
- byte[]与Texture2D(图片)互转
更多常用WEBService:http://www.webxml.com.cn/zh_cn/web_services.aspx
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml;
- using System.IO;
-
- public class HTTPDemo : MonoBehaviour
- {
- public string HostName = "http://www.webxml.com.cn";
-
- public string URLPath = "/WebServices/WeatherWebService.asmx/getWeatherbyCityName";
-
- private string PictureName = "/WebServices/ValidateCodeWebService.asmx/cnValidateImage?byString='Picture'";
-
- private string PictureByteName = "/WebServices/ValidateCodeWebService.asmx/cnValidateByte?byString='picByte'";
-
- private Texture2D mPicture;
- private Texture2D mPictureByte;
- private Texture2D mConvertPNG;
-
- public string[] Parameters = new string[] { "theCityName" };
-
- private string XMLContent = "null";
-
- public string testC = "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, 80, 100 + getJindu() * 100, 20), (getJindu() * 100) + "%");
-
-
-
- if (GUI.Button(new Rect(10, 130, 100, 60), "get"))
- {
- getWeatherbyCityName("58367");
- }
- GUI.Button(new Rect(120, 150, 100 + getJindu() * 100, 20), (getJindu() * 100) + "%");
-
-
-
-
- GUI.Label(new Rect(10, 220, 380, 500), mContent);
-
-
-
-
- if (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(PictureName);
- }
- GUI.Label(new Rect(100, 760, 200, 200), mPicture);
-
-
-
- if (GUI.Button(new Rect(350, 750, 80, 60), "downPicByte"))
- {
- downloadPictureByte(PictureByteName);
- }
- GUI.Label(new Rect(450, 760, 200, 200), mPictureByte);
- }
-
- public void postWeatherbyCityName(string str)
- {
-
- Dictionary<string, string> dic = new Dictionary<string, string>();
-
-
- dic.Add(Parameters[0], str);
-
- StartCoroutine(POST(HostName + URLPath , dic));
- }
-
- public void getWeatherbyCityName(string str)
- {
-
- Dictionary<string, string> dic = new Dictionary<string, string>();
-
-
- dic.Add(Parameters[0], str);
-
- StartCoroutine(GET(HostName + URLPath , dic));
- }
-
-
- public void downloadPicture(string picName)
- {
- testC ="picurl = " + picName;
-
- StartCoroutine(GETTexture(HostName + picName));
- }
-
-
- public void downloadPictureByte(string picName)
- {
- StartCoroutine(GETTextureByte(HostName + picName));
- }
-
-
-
- private float mJindu = 0;
- private string mContent;
-
- public float getJindu()
- {
- return mJindu;
- }
-
-
- IEnumerator POST(string url, Dictionary<string, string> post)
- {
-
- WWWForm form = new WWWForm();
-
- foreach (KeyValuePair<string, string> post_arg in post)
- {
- form.AddField(post_arg.Key, post_arg.Value);
- }
-
- WWW www = new WWW(url, form);
-
- yield return www;
- mJindu = www.progress;
-
- if (www.error != null)
- {
-
- mContent = "error :" + www.error;
- }
- else
- {
-
- mContent = www.text;
- }
- }
-
-
- IEnumerator GET(string url, Dictionary<string, string> get)
- {
- string Parameters;
- bool first;
- if (get.Count > 0)
- {
- first = true;
- Parameters = "?";
-
- foreach (KeyValuePair<string, string> post_arg in get)
- {
- if (first)
- first = false;
- else
- Parameters += "&";
-
- Parameters += post_arg.Key + "=" + post_arg.Value;
- }
- }
- else
- {
- Parameters = "";
- }
-
- testC ="getURL :" + Parameters;
-
-
- WWW www = new WWW(url + Parameters);
- yield return www;
- mJindu = www.progress;
-
- if (www.error != null)
- {
-
- mContent = "error :" + www.error;
- }
- else
- {
-
- mContent = www.text;
- }
- }
-
- IEnumerator GETTexture(string picURL)
- {
- WWW wwwTexture = new WWW(picURL);
-
- yield return wwwTexture;
-
- if (wwwTexture.error != null)
- {
-
- Debug.Log("error :" + wwwTexture.error);
- }
- else
- {
-
- mPicture = wwwTexture.texture;
- }
- }
-
- string PicByte;
- IEnumerator GETTextureByte(string picURL)
- {
- WWW www = new WWW(picURL);
-
- yield return www;
-
- if (www.error != null)
- {
-
- Debug.Log("error :" + www.error);
- }
- else
- {
-
- Debug.Log("PicBytes text = " + www.text);
-
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(new StringReader(www.text));
-
-
- PicByte = xmlDoc.GetElementsByTagName("base64Binary").Item(0).InnerText;
- testC = PicByte;
-
- mPictureByte = BttetoPic(PicByte);
- }
- }
-
-
- string AnalysisXML()
- {
- string str = "";
-
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(new StringReader(mContent));
-
-
-
-
- XmlNodeList nodes = xmlDoc.GetElementsByTagName("string");
-
-
- str += "item[1] = " + xmlDoc.GetElementsByTagName("string").Item(1).InnerText + "\n\n";
-
-
- foreach (XmlElement element in nodes)
- {
- if (element.Name == "string")
- {
- str += element.InnerText + "\n";
- }
- }
-
- return str;
- }
-
-
- public void convertPNG(Texture2D pic)
- {
- byte[] data = pic.EncodeToPNG();
- Debug.Log("data = " + data.Length + "|" + data[0]);
- mConvertPNG = new Texture2D(200, 200);
- mConvertPNG.LoadImage(data);
- }
-
-
- Texture2D BttetoPic(string base64)
- {
- Texture2D pic = new Texture2D(200,200);
-
- byte[] data = System.Convert.FromBase64String(base64);
-
- pic.LoadImage(data);
-
- string base64str = System.Convert.ToBase64String(data);
- Debug.Log("base64str = " + base64str);
-
- return pic;
- }
- }