Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 84 lines (57 sloc) 2.663 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Json;
using System.Threading.Tasks;

namespace OAuthMessageHandler.Client {

    class Program {

        static string _address =
            "http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=tourismgeek&count=5";

        static void Main(string[] args) {

            // Create client and insert an OAuth message handler in the message path that
            // inserts an OAuth authentication header in the request
            HttpClient _client = new HttpClient(
                new OAuthMessageHandler.Core.OAuthMessageHandler(
                    new HttpClientHandler()
                )
            );

            #region _w/o await

            //_client.GetAsync(_address).ContinueWith(requestTask => {

            // // Get HTTP response from completed task.
            // HttpResponseMessage response = requestTask.Result;

            // // Check that response was successful or throw exception
            // response.EnsureSuccessStatusCode();

            // // Read response asynchronously as JsonValue and write out tweet texts
            // response.Content.ReadAsAsync<JsonArray>().ContinueWith(readTask => {

            // JsonArray statuses = readTask.Result;
            // Console.WriteLine("\nLast 5 statuses from ScottGu's twitter account:\n");
            // foreach (var status in statuses) {

            // Console.WriteLine(status["text"] + "\n");
            // }

            // });

            //});

            #endregion

            #region _with await

            readResponseAsync(_client);

            #endregion

            Console.WriteLine("Hit ENTER to exit...");
            Console.ReadLine();
        }

        private static async Task<HttpResponseMessage> getResponseAsync(HttpClient httpClient) {

            var response = await httpClient.GetAsync(_address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            return response;
        }

        private static async void readResponseAsync(HttpClient httpClient) {

            var response = await getResponseAsync(httpClient);
            var statuses = await response.Content.ReadAsAsync<JsonArray>();

            Console.WriteLine("\nLast 5 statuses from ScottGu's twitter account:\n");
            foreach (var status in statuses) {

                Console.WriteLine(status["text"] + "\n");
            }
        }
    }
}
Something went wrong with that request. Please try again.