Page 1 of 1

C# - Migrate from WebClient To HttpClient

Posted: 9. Dec 2024, 16:58
by wilkax
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! :-)

Re: C# - Migrate from WebClient To HttpClient

Posted: 9. Dec 2024, 19:38
by admin
Thanks for sharing ...

Kind Regards
Kevin