当前位置:网站首页>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
边栏推荐
- 【机器人学习】机械臂抓手matlab运动学与admas动力学分析
- 【C】 Array
- Summary of SAP localized content in China
- Redis configuration cache expiration listening event trigger
- 微信为之疯狂的Glide使用——之生命周期学习
- STC单片机驱动1.8‘TFT SPI屏幕演示示例(含资料包)
- 13_ UE4 advanced_ Montage animation realizes attack while walking
- 01-SDRAM:初始化模块的代码
- Linux下安装MySQL8.0的详细步骤
- Military product development process - transition phase
猜你喜欢

西瓜书学习第六章---SVM

01-SDRAM:初始化模块的代码

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“

Linux下安装MySQL8.0的详细步骤

复现20字符短域名绕过以及xss相关知识点

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

Hangao database best practice configuration tool Hg_ BP log collection content

数字图像处理 第10章——图像分割

Alibaba Sentinel - 工作流程及原理解析
随机推荐
Why did I choose the test when the development salary was high?
西瓜书学习第六章---SVM
12_ UE4 advanced_ Change a more beautiful character model
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
Flask的创建的流程day05-06之创建项目
复现20字符短域名绕过以及xss相关知识点
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
"PHP Basics" output approximate value of PI
MySQL operation database data error: fatal error encoded during command execution
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机
Photo scale correction tool: DxO viewpoint 3 direct mount version
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
【FreeSwitch开发实践】UniMRCP编译与安装
美联储再加息,75基点 鲍威尔“放鸽”,美股狂欢
「PHP基础知识」输出圆周率的近似值
Codeworks 5 questions per day (average 1500) - day 25
Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
MYSQL入门与进阶(十四)
Principle knowledge is useful