コード例 #1
1
ファイル: Program.cs プロジェクト: prompt/preprompt-wcf-http
        private static void twitterSample()
        {
            var client = new HttpClient(Urls.Twitter)
            {
                Channel = new HttpMessageInspector(new WebRequestChannel())
            };

            var request = new HttpRequestMessage(HttpMethod.Get, "1/users/show/{0}.xml".FormatWith("duarte_nunes"));
            request.With().Accept("application/xml");

            HttpResponseMessage response = client.Send(request);
            response.EnsureSuccessStatusCode();

            Console.WriteLine(response.Content.ReadAsString());

            XElement element = response.Content.ReadAsXElement();
            Console.WriteLine(element.Element("description").Value);

            var formatter = new JsonDataContractFormatter(new DataContractJsonSerializer(typeof(TwitterUser)));
            var c = new RestyClient<TwitterUser>(Urls.Twitter, new[] { formatter });
            Tuple<HttpStatusCode, TwitterUser> result = c.ExecuteGet("1/users/show/{0}.json".FormatWith("duarte_nunes"));
            Console.WriteLine(result.Item1);
            Console.WriteLine(result.Item2.Description);
            Console.WriteLine(result.Item2.ProfileImageUrl);
        }
コード例 #2
0
        public Task GetTwitterUserMessage(HttpResponseMessage resp, string user)
        {
            var tcs = new TaskCompletionSource<object>();

            var formatter = new JsonDataContractFormatter(new DataContractJsonSerializer(typeof(TwitterUser)));
            new RestyClient<TwitterUser>(Urls.Twitter, new[] { formatter })
                .ExecuteGetAsync("1/users/show/{0}.json".FormatWith(user))
                .ContinueWith(t1 =>
                {
                    if (faulted(() => t1.Result.Item1, resp, tcs))
                    {
                        return;
                    }

                    new HttpClient().GetAsync(t1.Result.Item2.ProfileImageUrl)
                        .ContinueWith(t2 =>
                        {
                            if (faulted(() => t2.Result.StatusCode, resp, tcs))
                            {
                                return;
                            }

                            resp.Content = new StreamContent(t2.Result.Content.ContentReadStream);
                            resp.StatusCode = t2.Result.StatusCode;
                            tcs.SetResult(null);
                        });
                });
            return tcs.Task;
        }