Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace MonoCSharpBug
- {
- class MainClass
- {
- private static readonly HttpClient _httpClient = new HttpClient
- {
- BaseAddress = new Uri("http://ip.jsontest.com/"),
- };
- private static async Task<TResponse> SendAsync<TResponse>(HttpMethod httpMethod)
- {
- var requestUri = "/";
- using (var httpRequestMessage = new HttpRequestMessage(httpMethod, requestUri))
- {
- var httpResponseMessage = await _httpClient.SendAsync(httpRequestMessage);
- return await ReadResponseAsync<TResponse>(httpResponseMessage);
- }
- }
- private static async Task<TResponse> ReadResponseAsync<TResponse>(HttpResponseMessage httpResponseMessage)
- {
- var response = await httpResponseMessage.Content.ReadAsAsync<dynamic>();
- switch (httpResponseMessage.StatusCode)
- {
- case HttpStatusCode.Accepted:
- case HttpStatusCode.Created:
- case HttpStatusCode.NonAuthoritativeInformation:
- case HttpStatusCode.OK:
- case HttpStatusCode.PartialContent:
- return response.ToString();
- case HttpStatusCode.NoContent:
- return default(TResponse);
- default:
- {
- return default(TResponse);
- }
- }
- }
- public static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- var res = SendAsync<string>(HttpMethod.Get);
- var f = res.Result;
- Console.WriteLine(f);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment