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

Multithreading C# : Part 01

Multithreading with C# Think about old days, you have one central processing unit (CPU) in your pc which is capable of executing one operation at a time. If we have 5 operations to run then we have to wait till one operation is completed and then only other one can start. What will happen if the running operation has a bug and got stuck, then whole computer going to be freeze and useless unless we restart it. This is a huge problem. So if we can run multiple operation in the same time that would be great because it will solve this problem.

SQLite Database for Windows 8 App : Adding existing SQLite DB file

Hi Guys ... This Post is about adding an existing SQLite database file to the Windows Metro App project. As I think "using SQLite" is the best way to create local database for Windows Rt applications.Actually SQLite is very easy to learn but I really think if Microsoft SQL Server CE supports windows rt applications it will more fun. .Isnt it ?. :)

Start button confirmed to make a comeback in Windows 8.1

We first  heard rumors  about a possible comeback of the Start menu button in Windows 8.1 last week, but now sources speaking to  The Verge  have confirmed that this will indeed be the case, only it’s probably not what most detractors were hoping for. The newly reintroduced button will reportedly sit on the traditional bottom left corner, and will look near-identical to the existing Windows flag used in the Charm bar, but clicking on it will simply bring up the tile-based Start screen rather than the old Start menu. Read Full Article