unity c#请求http协议

  IEnumerator GetCodeRequest(string time,string server_Id,int role_id,string code,string sign,string account, Action<int, string> action)
        {
            if (code.Length>0)
            {
            
                var url = string.Format("http://111.111.1.111/index.php/appapi/game/codepost?time={0}&server_id={1}&role_id={2}&code={3}&sign={4}&account={5}",time,server_Id,role_id,code,sign,account);
                var webRequest = UnityWebRequest.Get(url);
                yield return webRequest.SendWebRequest(); //此处需要等待请求消息的返回 才能走接下来的回调

                if (webRequest.isHttpError || webRequest.isNetworkError)
                {
                    Debug.Log(webRequest.error);
                }
                else
                {
                    Debug.Log(webRequest.downloadHandler.text);
                    CodeResult result = JsonUtility.FromJson<CodeResult>(webRequest.downloadHandler.text);
                    Debug.Log(webRequest.downloadHandler.text);
                    //List<LoginAddress> json_object = list.LoginAddressJson;
                    Debug.Log(webRequest.downloadHandler.text);
                    if (action != null)
                    {
                      action(result.code, result.msg);
                    }
                }
            }
        }

 

posted @ 2021-08-04 09:29  奕心1999  阅读(285)  评论(0)    收藏  举报