当前位置:网站首页>C obtains JSON format data asynchronously from the web address
C obtains JSON format data asynchronously from the web address
2022-07-29 03:17:00 【Peacock Flying Southeast - Shenzhen】
1 Start visiting
public static void StartAccessingURL(string url,int index)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "Get";
webRequest.ContentType = "application/json";
webRequest.Headers.Add("Authorization", "MzQ1"); //token
webRequest.BeginGetResponse(new AsyncCallback(OnOneResponse), webRequest);
}
catch(Exception ex)
{
Log.LogHelper.Error("StartAccessingURL() " + ex.Message);
}
}2 Callback function
public static void OnOneResponse(IAsyncResult ar)
{
try
{
WebRequest wrq = (WebRequest)ar.AsyncState;
WebResponse wrs = wrq.EndGetResponse(ar);
HttpWebResponse httpWebResponse = (HttpWebResponse)wrs;
StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string data = reader.ReadToEnd();
reader.Close();
httpWebResponse.Close();
getData(data);
}
catch(Exception e)
{
Log.LogHelper.Error("OnOneResponse" + e.Message);
}
}3 Get data
public static void getData(string str)
{
try
{
OrderItem oi = new OrderItem();
JObject ja = (JObject)JsonConvert.DeserializeObject(str);
string json = ja.Last.ToString();
setDoubleToSingle(ref json);
var resultObjects = AllChildren(JObject.Parse(json)).
First(c => c.Type == JTokenType.Array && c.Path.Contains("data")).
Children<JObject>();
foreach (JObject result in resultObjects)
{
oi = JsonConvert.DeserializeObject<OrderItem>(result.ToString());
observation.Add(oi);
}
}
catch (Exception e)
{
Log.LogHelper.Error("getData() " + e.Message);
}
}4 other
#region Traverse
private static IEnumerable<JToken> AllChildren(JToken json)
{
foreach (var c in json.Children())
{
yield return c;
foreach (var cc in AllChildren(c))
{
yield return cc;
}
}
}
#endregion
#region Change double quotation marks into single quotation marks
public static void setDoubleToSingle(ref string str)
{
str = str.Replace("\r\n", "");
str = str.Replace("\"", "'");
str = str.Insert(0, "{");
str = str.Insert(str.Length, "}");
}
#endregion
边栏推荐
- Why did I choose the test when the development salary was high?
- Typescript learning (I)
- 2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
- Redis之sentinel哨兵集群怎么部署
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
- C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
- Production deployment zabbix5.0 notes
- MySQL operation database data error: fatal error encoded during command execution
- C陷阱和缺陷 第3章 语义“陷阱” 3.2 非数组的指针
猜你喜欢

【FreeSwitch开发实践】media bug获取通话语音流

简历竟然敢写精通并发编程,那你说说AQS为什么要用双向链表?

Redis之sentinel哨兵集群怎么部署

Summary of basic knowledge points of C language

What is eplato cast by Plato farm on elephant swap? Why is there a high premium?

单例模式(饿汉式 懒汉式)

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

Unity game special effects

国产ERP有没有机会击败SAP ?

13_ue4进阶_蒙太奇动画实现一边走一边攻击
随机推荐
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Unity 之游戏特效
2022-07-28 study notes of group 4 self-cultivation class (every day)
Data truncation and estimation
How to solve the time zone problem in MySQL timestamp
Sanzi chess (player + computer)
2022-07-28 第四小组 修身课 学习笔记(every day)
C陷阱与缺陷 第2章 语法“陷阱” 2.6 “悬挂”else引发的问题
Chapter 09_ Use of performance analysis tools
MySQL流程控制之while、repeat、loop循环实例分析
美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
ShardingSphere之水平分表实战(三)
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
Summary of SAP localized content in China
生产部署zabbix5.0笔记
What if MySQL forgets the password
"PHP Basics" output approximate value of PI
MySQL installation and configuration super detailed tutorial and simple database and table building method
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
Military product development process - transition phase