自己寫的測試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;
- }
- }