Skip to content

Replace WebClient and WebRequest to HttpClient #515

@frg2089

Description

@frg2089

The following APIs are marked as obsolete, starting in .NET 6. Using them in code generates warning SYSLIB0014 at compile time.

SYSLIB0014 - Microsoft Learn

Add System.Net.Http.Json package to polyfill GetFromJsonAsync methods.

like this:

WebClient client = new WebClient();
Stream data = client.OpenRead("http://api.cncnet.org/status");
string info = string.Empty;
using (StreamReader reader = new StreamReader(data))
{
info = reader.ReadToEnd();
}
info = info.Replace("{", String.Empty);
info = info.Replace("}", String.Empty);
info = info.Replace("\"", String.Empty);
string[] values = info.Split(new char[] { ',' });
int numGames = -1;
foreach (string value in values)
{
if (value.Contains(cncnetLiveStatusIdentifier))
{
numGames = Convert.ToInt32(value.Substring(cncnetLiveStatusIdentifier.Length + 1));
return numGames;
}
}
return numGames;

// TODO: Cached it!
// See https://learn.microsoft.com/dotnet/api/system.net.http.httpclient.-ctor?view=net-8.0#remarks
HttpClient client = new();
// TODO: make it async.
var infos = client.GetFromJsonAsync<Dictionary<string, int>>("http://api.cncnet.org/status").Result;
if (!infos.TryGetValue(cncnetLiveStatusIdentifier, out int numGames))
    numGames = -1;

return numGames;

TODO

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions