C# - Migrate from WebClient To HttpClient

Pushsafer make it easy and safe to get push-notifications in real time on your Android or Windows mobile device, iPhone, iPad and Desktop!

Mit Pushsafer kann man in Echtzeit, einfach und sicher, Push-Benachrichtigungen auf sein iPhone, iPad, Android oder Windows Gerät sowie auf den Desktop senden lassen!

======================
Google Play
App Store
Windows Store
Homepage
wilkax
Posts: 1
Joined: 9. Dec 2024, 16:51

C# - Migrate from WebClient To HttpClient

Postby wilkax » 9. Dec 2024, 16:58

Hello there,

Due to WebClient being marked as "deprecated" consider migrating to HttpClient if you use the Pushsafer API in C#.

For you all, here is how:

Transform this...

Code: Select all

var options= new NameValueCollection {
		{ "t", title },
		{ "m", message },
		{ "s", sound },
		{ "v", vibration },
		{ "i", icon },
		{ "c", iconcolor },
		{ "d", device },
		{ "u", url },
		{ "ut", urltitle },
		{ "p", picture },
		{ "k", private_key }
};
using (var client = new WebClient())
{
    client.UploadValues("https://www.pushsafer.com/api", options);
}
...to this:

Code: Select all

                NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
                queryString.Add("t", title);
                queryString.Add("m", text);
                queryString.Add("i", icon);
                queryString.Add("c", iconcolor);
                queryString.Add("d", device);
                .....
                queryString.Add("k", private_key);

                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync($"https://www.pushsafer.com/api?{queryString}");
                }
You're welcome! :-)
Last edited by wilkax on 8. Jan 2025, 12:33, edited 1 time in total.

User avatar
admin
Site Admin
Posts: 986
Joined: 1. Dec 2014, 12:41
Location: Germany

Re: C# - Migrate from WebClient To HttpClient

Postby admin » 9. Dec 2024, 19:38

Thanks for sharing ...

Kind Regards
Kevin
Pushsafer DE: Dir gefällt Service und Support! Dann freuen wir uns auf deine Bewertung!
Pushsafer EN: You like Service and Support! Then we look forward to your review!

Pushsafer.com Website - iOS - Android - Windows 10


Who is online

Users browsing this forum: No registered users and 3 guests