Atif Shabeer
Atif Shabeer

Reputation: 555

Windows.Web.Http.HttpClient in Windows Phone 8.1

I am writing a Windows Phone 8.1 App (WINRT). I have created a Login page and want to post login data to server and get response.

Thanks to @Kai Brummund who gave me this following method but it uses old using System.Net.Http; But I want to use highly recommended Windows.Web.Http.HttpClient

Using the new namespace, its giving me error everywhere in this method. What changes I need to make?

 CancellationTokenSource cancellationToken;
        public async void ReadHeaders()
        {
            cancellationToken = new CancellationTokenSource();

            // Create the message
            string address = singletonInstance.APIServer + "MkhAdapter/rest/mkh/Login";
            var message = new HttpRequestMessage(HttpMethod.Post, new Uri(address));

            // Add the message body
            message.Content = new StringContent(
               LoginJsonData,
                System.Text.UTF8Encoding.UTF8, "application/json");

            ////filter/compress
            //HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();

            // Send
            var client = new HttpClient();
            var result = await client.SendAsync(message, cancellationToken.Token);

            result.EnsureSuccessStatusCode();

            // Get ids from result
            var finalresult = await result.Content.ReadAsStringAsync();
        } 

Upvotes: 0

Views: 566

Answers (1)

Kai Brummund
Kai Brummund

Reputation: 3568

Basically you want to do an Http Post request, right? You can just use WebClient:

        // Create the message
        var message = new HttpRequestMessage(HttpMethod.Post, new Uri(singletonInstance.APIServer + "MkhAdapter/rest/mkh/Login"));

        // Add the message body
        message.Content = new StringContent(
            "{'UserCategory': 'user','Email': User_EnteredUsername, 'Password':User_EnteredPassword}",
            System.Text.UTF8Encoding.UTF8, "application/json");

        // Send
        var client = new HttpClient();
        var result = await client.SendAsync(message, cancellationToken);

        result.EnsureSuccessStatusCode();

        // Get ids from result
        return (await result.Content.ReadAsStringAsync());

Upvotes: 1

Related Questions