惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
I
Intezer
S
Schneier on Security
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
月光博客
月光博客
G
Google Developers Blog
S
Securelist
WordPress大学
WordPress大学
T
Tailwind CSS Blog
IT之家
IT之家
C
Cybersecurity and Infrastructure Security Agency CISA
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
V
Vulnerabilities – Threatpost
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Cloudflare Blog
P
Privacy International News Feed
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
爱范儿
爱范儿
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
有赞技术团队
有赞技术团队
博客园 - 司徒正美
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
小众软件
小众软件
T
Tenable Blog
Jina AI
Jina AI
Simon Willison's Weblog
Simon Willison's Weblog
Security Latest
Security Latest
Application and Cybersecurity Blog
Application and Cybersecurity Blog
AWS News Blog
AWS News Blog
T
Troy Hunt's Blog
博客园 - Franky
量子位
博客园_首页
P
Proofpoint News Feed

博客园 - 威少小二orz

团结引擎抖音小游戏一定要手动填缓存资源域名 团结引擎+Addressable+Instant Game打包抖音小游戏 团结引擎发布抖音小游戏(十万个坑已踩完) 团结引擎发布小游戏区分不同平台 团结引擎发布小游戏与js版本SDK的互相调用 Unity发布京东小游戏 LayerMask的使用规范 Unity RectTransform中使用stretch模式时代码动态控制Left、Top、Right、Bottom Unity使用https请求握手失败的处理方案 【MAUI开发】一、初识MAUI Android中使用GSON解析JSON数据 游戏软著渠道的用户协议和隐私协议方案(启动显示方案) C#文件夹复制 C#在Json序列化中动态忽略某些属性或字段 安卓多个Activity大退时自动重启的问题处理 C#中接收到的Json数据中key为数字的问题处理 对于UniWebView这种组件违规获取信息的处理 TapTap实名认证-Android Unity获取手机本地应用以及调起apk安装、启动app、安装完成回调 Unity接入穿山甲GroMore广告——重构偏2(Banner广告)
Unity使用Get和Post传递json数据并转换成class对象
威少小二orz · 2023-01-03 · via 博客园 - 威少小二orz

此文写的并不是使用UnityRequest等等Unity自带的方法。

1、Get方法

        public static async Task<string> DoGet(string requestUrl, string parms)
        {
            string reslut = "";
            string uri = ""; // url + "?" + parms;
            if (string.IsNullOrEmpty(parms))
            {
                uri = requestUrl;
            }
            else
            {
                uri = requestUrl + "?" + parms;
            }

            string responseBody = string.Empty;
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Add("Method", "Get");
                httpClient.DefaultRequestHeaders.Add("KeepAlive", "false");
                httpClient.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

                HttpResponseMessage response = await httpClient.GetAsync(uri);
                response.EnsureSuccessStatusCode();
                responseBody = await response.Content.ReadAsStringAsync();
            }
            return responseBody;
        }

  调用:

  string u2 = "http://" + ip + "/api/WeatherForecast/GetGameList";
            string data2 = await HttpHelper.DoGet(u2, "");
 类名 info = JsonConvert.DeserializeObject<类名>(data2);

2、Post

        public static async Task<string> DoPost(string Url, object message)
        {
            try
            {
                //var res = new T();
                string jsonContent = message == null ? "" : JsonConvert.SerializeObject(message);
                string responseBody = string.Empty;
                using (HttpClient httpClient = new HttpClient())
                {
                    var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                    httpClient.DefaultRequestHeaders.Add("Method", "Post");

                    HttpResponseMessage response = await httpClient.PostAsync(Url, content);
                    response.EnsureSuccessStatusCode();
                    responseBody = await response.Content.ReadAsStringAsync();
                    //res = (T)JsonConvert.DeserializeObject<T>(responseBody);
                    return responseBody;
                }
            }
            catch (Exception ex)
            {

                return "";
            }


        }

调用:

        string js = await HttpHelper.DoPost(@"https://****", null);
            //JObject? result = JsonConvert.DeserializeObject<JObject>(js);
            类名 info = JsonConvert.DeserializeObject<类名>(js);

这样写主要是省了回调,省得使用消息者模式消息满天飞。

注意使用await等待时,主线程不会锁死,而使用await时,当前的方法前要加上async,例子如下。

      public async void GetPostData()
        {
            string js = await HttpHelper.DoPost(@"***********", null);
            //JObject? result = JsonConvert.DeserializeObject<JObject>(js);
            PkgInfo info = JsonConvert.DeserializeObject<PkgInfo>(js);
        }

在把json数据转换成类对象之前,如果需要校验,则使用如下方式:

        public async void GetPostData()
        {
            string js = await HttpHelper.DoPost(@"***********************", null);
            JObject result = JsonConvert.DeserializeObject<JObject>(js);
            string errcode = result["errcode"]?.ToString();
            string errmsg = result["errmsg"]?.ToString();
        //先验证errcode之后,再根据情况进行类对象的转换。 PkgInfo info
= JsonConvert.DeserializeObject<PkgInfo>(js); }