Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert WebClient to HttpClient

I am trying to convert a WebClient I used to use on a Win7 project to a HttpClient to use it on my Win8.1 system.

WenClient:

public static void PastebinSharp(string Username, string Password)
        {
            NameValueCollection IQuery = new NameValueCollection();

            IQuery.Add("api_dev_key", IDevKey);
            IQuery.Add("api_user_name", Username);
            IQuery.Add("api_user_password", Password);

            using (WebClient wc = new WebClient())
            {
                byte[] respBytes = wc.UploadValues(ILoginURL, IQuery);
                string resp = Encoding.UTF8.GetString(respBytes);

                if (resp.Contains("Bad API request"))
                {
                    throw new WebException("Bad Request", WebExceptionStatus.SendFailure);
                }
                Console.WriteLine(resp);
                //IUserKey = resp;
            }
        }

And this is my first shot on HttpClient

public static async Task<string> PastebinSharp(string Username, string Password)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("api_dev_key", GlobalVars.IDevKey);
                client.DefaultRequestHeaders.Add("api_user_name", Username);
                client.DefaultRequestHeaders.Add("api_user_password", Password);

                using (HttpResponseMessage response = await client.GetAsync(GlobalVars.IPostURL))
                {
                    using (HttpContent content = response.Content)
                    {
                        string result = await content.ReadAsStringAsync();
                        Debug.WriteLine(result);
                        return result;
                    }
                }
            }
        }

My HttpRequest returns Bad API request, invalid api option while my WebClient returns a successful response.

How should this be done?

I understand of course I am adding headers instead of query, but I have no idea how to add queries...

like image 558
John P. Avatar asked Feb 04 '17 22:02

John P.


Video Answer


1 Answers

The msdn page of UploadValues say that WebClient send data in a POST request with application/x-www-form-urlencoded Content-type. So you must/can use FormUrlEncodedContent http content.

public static async Task<string> PastebinSharpAsync(string Username, string Password)
{
    using (HttpClient client = new HttpClient())
    {
        var postParams = new Dictionary<string, string>();

        postParams.Add("api_dev_key", IDevKey);
        postParams.Add("api_user_name", Username);
        postParams.Add("api_user_password", Password);

        using(var postContent = new FormUrlEncodedContent(postParams))
        using (HttpResponseMessage response = await client.PostAsync(ILoginURL, postContent))
        {
            response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
            using (HttpContent content = response.Content)
            {
                string result = await content.ReadAsStringAsync();
                Debug.WriteLine(result);
                return result;
            }
        }
    }
}
like image 55
Kalten Avatar answered Oct 27 '22 20:10

Kalten