Created
April 26, 2015 15:15
-
-
Save papeMK2/7551da364e3e87393eb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Net; | |
using Newtonsoft.Json; | |
using System.Net.Http; | |
namespace HttpClientWrapper | |
{ | |
public static class HttpOperations<Type> | |
{ | |
private static HttpClient client = new HttpClient(); | |
/// <summary> | |
/// HttpGetを行う | |
/// </summary> | |
/// <param name="url">Getするurl</param> | |
/// <returns>Getした結果</returns> | |
public static async Task<Type> HttpGetAsync(string url) | |
{ | |
try | |
{ | |
var response = await client.GetAsync(url); | |
return await ParseJson(await response.Content.ReadAsStringAsync()); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// サーバからファイルをダウンロードする | |
/// </summary> | |
/// <param name="url"></param> | |
public static async Task<bool> HttpDownload(string url, string filename) | |
{ | |
try | |
{ | |
var response = await client.GetAsync(url); | |
var file = await response.Content.ReadAsStreamAsync(); | |
var outputStream = new FileStream(filename, FileMode.Create); | |
await file.CopyToAsync(outputStream); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
return false; | |
} | |
} | |
/// <summary> | |
/// サーバに対してデータをPostする | |
/// </summary> | |
/// <param name="url">postするurl</param> | |
/// <param name="content">postするデータ</param> | |
/// <returns>サーバレスポンスとマッピングしたモデルインスタンス</returns> | |
public static async Task<Type> HttpPostAsync(string url, HttpContent content) | |
{ | |
try | |
{ | |
var response = await client.PostAsync(url, content); | |
return await ParseJson(await response.Content.ReadAsStringAsync()); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// HttpPutをする | |
/// </summary> | |
/// <param name="url">Putするurl</param> | |
/// <param name="content">Putするデータ</param> | |
/// <returns>結果のレスポンス</returns> | |
public static async Task<Type> HttpPutAsync(string url, HttpContent content) | |
{ | |
try | |
{ | |
var response = await client.PutAsync(url, content); | |
return await ParseJson(await response.Content.ReadAsStringAsync()); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// Jsonを決められた型に整形して返す | |
/// </summary> | |
/// <param name="json">jsonの文字列</param> | |
/// <returns>jsonとマッピングしたモデルインスタンス</returns> | |
private static async Task<Type> ParseJson(string json) | |
{ | |
return await JsonConvert.DeserializeObjectAsync<Type>(json); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment