Skip to main content

HttpClient Request for Windows Phone 8.1 C#


HttpClient Request

HttpClient class provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.


Namespace: Windows.Web.Http;
Assembly:  Windows.Web.Http (in Windows.Web.Http.dll)
 
 
 
 
 
 
 
Simple HttpClient Request  GET
 
This is a simple example for get a string as a response, from HttpClient request.
 
using Windows.Web.Http;
 
public async void GetaString()
{
    try 
    {
        //Create HttpClient
        HttpClient httpClient = new HttpClient();
 
       //Define Http Headers
       httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
 
        //Call
        string ResponseString = await httpClient.GetStringAsync(
            new Uri("http://www.csharpteacher.com/Api/getinfo")); 
            //Replace current URL with your URL
     }
 
     catch(Exception ex)
     {
         //....
     }
}
 
Get full response HttpClient Request  GET
 
If you want to get all the information ( like Headers, Response codes  etc..) with response then this method is the suitable one.

public async void GetFullResponse()
{
    try
    {
        //Create Client 
        var client = new HttpClient();
 
        //Define URL. Replace current URL with your URL
//Current URL is not a valid one
        var uri = new Uri("http://www.csharpteacher.com/Api/getinfo");          //Call. Get response by Async         var Response = await client.GetAsync(uri);         //Result & Code         var statusCode = Response.StatusCode;         //If Response is not Http 200          //then EnsureSuccessStatusCode will throw an exception         Response.EnsureSuccessStatusCode();         //Read the content of the response.         //In here expected response is a string.         //Accroding to Response you can change the Reading method.         //like ReadAsStreamAsync etc..         var ResponseText = await Response.Content.ReadAsStringAsync();     }     catch(Exception ex)     {         //...     } }

HttpClient How to read response headers

public async void ReadHeaders()
{
    try 
    {
        //Create Client 
        var client = new HttpClient();
 
        //Define URL. Replace current URL with your URL
        //Current URL is not a valid one
        var uri = new Uri("http://www.csharpteacher.com/Api/getinfo");
 
        //Call. Get response by Async
        var Response = await client.GetAsync(uri);
 
        //If Response is not Http 200 
        //then EnsureSuccessStatusCode will throw an exception
        Response.EnsureSuccessStatusCode();
 
        //Grab each header one by one
        foreach (var header in Response.Headers)
        {
            //Header
            string headerItem = header.Key + " = " + header.Value;
 
            //Showing each header one by one in a MessageDialog
            showMessage(header.Key + " = " + header.Value);
        }
    }
    catch (Exception ex)
    {
        //...
    }
}
 
//Showing header in a MessageDialog
public async void showMessage(string header)
{
    MessageDialog message = new MessageDialog(header);
    await message.ShowAsync();
    
}


Hope this will helpful
Thank You :)

Comments

Popular posts from this blog

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup. 

Visual Studio 2013 New Editor Features

In Visual Studio 2013, we have introduced new features that boost productivity and save time when working inside the Editor. Some of these are new features and some are the most popular extensions from  Productivity Power Tools . These features are a result of the feedback you gave us through  User Voice  requests, forum posts and Connect bugs. The MVP community also helped us pick some of these experiences. Our primary focus for the Editor in this version is to keep the developer in context as much as possible. This blog post describes capabilities that bring information to your fingertips and allow you to do as much as possible without leaving your place in code.

C# Character Escape Sequences

Character combinations consisting of a backslash ( \ ) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.