try
{
string url = "http://192.168.127.245:8099/api/AlarmInfo/AlarmInfoGET?count=100";
System.Net.HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(url);
string strValue = "";
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
//strValue += StrDate + "\r\n";
}
List<AlarmInfo> model = JSONStringToList<AlarmInfo>(strValue);
//model = model.OrderBy(u => u.UpdateTime).ToList(); //这样我们也把他进行了一次排序
//model.Sort((AlarmInfo p1, AlarmInfo p2) => p1.UpdateTime.CompareTo(p2.UpdateTime));
model.Sort((AlarmInfo p1, AlarmInfo p2) => p2.UpdateTime.CompareTo(p1.UpdateTime)); //排序
model = model.Take(30).ToList(); //获取前30条数据
List<AlarmInfoMode> alarmInfoModes = new List<AlarmInfoMode>();
foreach (AlarmInfo item in model)
{
AlarmInfoMode alarmInfoMode = new AlarmInfoMode() {
_AlarmStations = GetAlarmStations(item.AssemblyLineId),
Id=item.Id,
UpdateTime=item.UpdateTime,
AlarmContent=item.AlarmContent,
IPAddress=item.IPAddress
};
alarmInfoModes.Add(alarmInfoMode);
}
this._DataGrid.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => //委托异步执行
{
this._DataGrid.ItemsSource = alarmInfoModes;
}));
}
catch (Exception err)
{
Logger.Error(err);
}
参考网址【.net core请求外部api接口_香煎三文鱼的博客-CSDN博客】
参考网址【C# 中 WebClient HttpClient 的区别】
.net core请求多个api【.NET CORE后台请求API_雨中深巷的油纸伞的博客-CSDN博客】