您好,登錄后才能下訂單哦!
using UnityEngine; using System; using System.Collections; using System.Net; using System.Text; using System.Web; public class GainToken : MonoBehaviour { // Use this for initialization void Start () { } public string content; private string GetToken() { string xml = "............";//提供的XML/Json數(shù)據(jù) try { byte[] data = Encoding.Default.GetBytes(xml); string url = "..........";//提供獲取Token值的服務(wù)地址(很重要) //如果不了解HttpWebRequest類,進(jìn)入網(wǎng)址https://msdn.microsoft.com/zh-cn/library/system.net.httpwebresponse.aspx查詢 HttpWebRequest requst = (HttpWebRequest)WebRequest.Create(url);//獲取 requst.Method = "POST";//請(qǐng)求服務(wù)的方式 System.IO.Stream sm = requst.GetRequestStream();//Web請(qǐng)求 sm.Write(data, 0, data.Length);//XML數(shù)據(jù) sm.Close(); HttpWebResponse response = (HttpWebResponse)requst.GetResponse();//響應(yīng)WebResponse從互聯(lián)網(wǎng)上的資源。 System.IO.Stream streamResponse = response.GetResponseStream(); //獲取到的Token值 System.IO.StreamReader streamRead = new System.IO.StreamReader(streamResponse, Encoding.UTF8); Char[] readBuff = new Char[256];//字節(jié)數(shù) int count = streamRead.Read(readBuff, 0, 256); //轉(zhuǎn)換Token值為string形式 while (count > 0) { string outputData = new string(readBuff, 0, count); content += outputData; count = streamRead.Read(readBuff, 0, 256); } response.Close();//關(guān)閉Token值請(qǐng)求(一定要關(guān)閉,要不然會(huì)程序會(huì)死掉) } catch (System.Exception ex) { ex.ToString(); } return content; } 接下來,通過獲取的token值,得到URL =“http://" + ip:port + "..." + token +"...."; 通過WWW類和協(xié)程獲取服務(wù)器上的數(shù)據(jù),解析。。。。。。 下面的東西就不說了,很簡單的!! 補(bǔ)充一個(gè)知識(shí):獲取下來的字符串有些是多余的,我們可以將多余的摘掉, public static string GetTokenXML(string str, string startStar, string endStr) { int start = str.IndexOf(startStar); int end = str.IndexOf(endStr); int leng = end - (start + startStar.Length); if (start == -1) { return ""; } else if (end == -1) { return ""; } else { return str.Substring(start + startStar.Length, leng); } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。