Kartiikeya
Kartiikeya

Reputation: 2516

Send request & Get Response in Windows Phone 8.1 WinRT

I have migrated from Windows Phone 8 Silverlight to Windows Phone 8.1 WinRT. HttpWebRquest and WebClient used in WP 8 Silverlight. Wp 8.0 silverlight ,i have used webclient..

 webClntRech.DownloadStringAsync(new Uri("http://wallet.net.co.in/services/bi/rechargedownload/01/0001/" + DateTime.Now.ToString("ddMMyyhhmmssms") + "/504434"));

            webClntRech.DownloadStringCompleted += webClntRech_DownloadStringCompleted;

I want similar functionality in WInrt 8.1

I can't find WebClient in WinRT. I came across HttpClient.

As based on requirement, we have to implement POST Method for all requests. I followed some examples and got this code..which is not working..

 HttpClient client = new HttpClient();
        string ResponceResult = await client.PostAsync("http://wallet.net.co.in/services/bi/rechargedownload/01/0001/" + DateTime.Now.ToString("ddMMyyhhmmssms") + "/504434",);

 MessageDialog m = new MessageDialog(ResponceResult);
        await m.ShowAsync();

The response will be in Json Format.

I'm not familiar with HTTP Content parameter of PostAynsc() Method.

Went through several links. Can't get any help. How to implement it..

Upvotes: 0

Views: 445

Answers (3)

Muhammad Hassan
Muhammad Hassan

Reputation: 1057

I use this utility method to POST JSON

 async void PostJson(string URL, string json)
 {
     HttpClient httpClient = new HttpClient();
     httpClient.Timeout = TimeSpan.FromMinutes(5);
     httpClient.MaxResponseContentBufferSize = 25600000;

     httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
     httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");

     HttpResponseMessage response = await httpClient.PostAsync(new Uri(URL), new StringContent(json, Encoding.UTF8, "application/json"));
     response.EnsureSuccessStatusCode();
     string responseAsText = await response.Content.ReadAsStringAsync();
     Dictionary<string, string> responseJson = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseAsText);
 }

Update

In order to just get JSON from URL, you can use the following code:

var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync(url);

// Deserialize JSON
MyObject myObject = JsonConvert.DeserializeObject<MyObject>(response);

Upvotes: 0

Akaya93
Akaya93

Reputation: 117

I used something like this with HttpClient

public async Task<string> httpClient(object param, Uri targetUri, string key)
    {
        using(HttpClient client = new HttpClient())
        {
            string jsonData = JsonConvert.SerializeObject(param);
            FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>(key, jsonData)
            });
            HttpResponseMessage response = await client.PostAsync(targetUri, content);
            string result = await response.Content.ReadAsStringAsync();
            return result;
        }
    }

you can modified it any way you want

I used Task instead of void so I can await the result and later I deserialized it in another method

Upvotes: 0

Umar Ali
Umar Ali

Reputation: 404

First of all you need to change your url i.e: http://services.groupkt.com/country/get/all

and then try this

sending an Http request to rest api on Windows Phone 8.1

i hope this is helpful for you

Upvotes: 1

Related Questions