Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace WebClient and WebRequest to HttpClient #515

Open
4 tasks
frg2089 opened this issue Mar 5, 2024 · 0 comments
Open
4 tasks

Replace WebClient and WebRequest to HttpClient #515

frg2089 opened this issue Mar 5, 2024 · 0 comments

Comments

@frg2089
Copy link
Contributor

frg2089 commented Mar 5, 2024

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

@frg2089 frg2089 changed the title Replace WebClient with HttpClient Replace WebClient and WebRequest to HttpClient Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant