using System.Collections; using UnityEngine; using UnityEngine.Networking; using LitJson; using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; public class HttpManager : MonoBehaviour { private static HttpManager instance; public static HttpManager Instance { get { if (instance == null) { instance = GameObject.Find("Managers").GetComponent(); } return instance; } } private string m_BaseUrl = "https://yqkfq.yueqing.gov.cn/yq-api/xt-sps/parkTwin/select"; private Dictionary m_textureCache = new Dictionary(); private void Start() { } public Texture2D GetTexture(string path) { Texture2D tex = null; if (m_textureCache.TryGetValue(path, out tex)) { return tex; } return null; } /// /// 获取天气 /// /// public IEnumerator GetWeather(Action complete) { UnityWebRequest request = new UnityWebRequest("https://yqkfq.yueqing.gov.cn/yq-api/xt-system/common/getWeather", UnityWebRequest.kHttpVerbPOST); string json = "{\"location_type\": 1, \"lat\": 28.066292, \"lng\": 120.990018 }"; DownloadHandler downloadHandler = new DownloadHandlerBuffer(); request.downloadHandler = downloadHandler; request.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); byte[] bodyRaw = Encoding.UTF8.GetBytes(json); request.uploadHandler = new UploadHandlerRaw(bodyRaw); UnityWebRequestAsyncOperation op = request.SendWebRequest(); while (!op.isDone) { yield return null; } if (string.IsNullOrEmpty(request.error)) { //Debug.Log(request.downloadHandler.text); JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text); WeatherInfo weatherInfo = new WeatherInfo(); weatherInfo.weatherType = jsonData["data"]["data"]["weather"]["weather"].ToString(); weatherInfo.temp = weatherInfo.weatherType + " " + jsonData["data"]["data"]["weather"]["temp"].ToString() + "℃"; //Debug.Log(request.downloadHandler.text); //获取天气 complete?.Invoke(weatherInfo); } } public IEnumerator LoadTextureAsync(string path, Action ac) { Texture2D tex = GetTexture(path); if (tex == null) { UnityWebRequest req = UnityWebRequest.Get(path); UnityWebRequestAsyncOperation op = req.SendWebRequest(); while (!op.isDone) { yield return null; } if (op.webRequest.isHttpError || op.webRequest.isNetworkError) { Debug.Log("Get web resource failed: " + path); Debug.Log("ErrorCode: " + op.webRequest.responseCode); yield break; } byte[] bytes = op.webRequest.downloadHandler.data; tex = new Texture2D(1, 1); if (!tex.LoadImage(bytes)) { Debug.Log("LoadImage error: " + path); yield break; } if (!m_textureCache.ContainsKey(path)) { m_textureCache.Add(path, tex); } } ac?.Invoke(tex); } /// /// 获取信息 /// /// /// /// public IEnumerator LoadTextAsync(string path, System.Action callback) { string result = null; UnityWebRequest req = UnityWebRequest.Get(path); UnityWebRequestAsyncOperation op = req.SendWebRequest(); while (!op.isDone) { yield return null; } if (op.webRequest.isHttpError || op.webRequest.isNetworkError) { Debug.Log("Get web resource failed: " + path); Debug.Log("ErrorCode: " + op.webRequest.responseCode); } else { result = op.webRequest.downloadHandler.text; } //Debug.Log(path + ":" + result); if (callback != null) { //Debug.Log(path + ":" + result); callback(result); } } public async void Down(System.Action callback) { string url = @"https://www.tianqi.com/pudong/"; string s = await GetPage(url, null); if(callback != null) { callback?.Invoke(s); } } public async Task GetPage(string url, string cookies) { return await Task.Run(() => { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"); //request.Referer = $"https://{AppValue.Host}/"; request.Headers.Add("Upgrade-Insecure-Requests", "1"); request.KeepAlive = true; //request.Headers.Add("Cookie", cookies); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); } return null; }); } /// /// 通过经纬度获取世界坐标 /// /// /// /// 3576.8 -1553.5 120.976111 121.021983 lng 3576.8f - (now - 120.976111f) / 0.045872f * 5130.3f /// -3207.6 3428.3 28.091056 28.038424 lat -3207.6f + (28.091056f - now) / 0.052632f * 6635.9f /// private Vector3 GetVectByLnat(double lng, double lat) { double x = 3576.8f - (lng - 120.976111f) / 0.045872f * 5130.3f; double z = -3207.6f + (28.091056f - lat) / 0.052632f * 6635.9f; return new Vector3((float)x, 45, (float)z); } } public class WeatherInfo { public string temp; public string weatherType; }