Instantly share code, notes, and snippets.
Created
May 1, 2014 15:42
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save papeMK2/c5dfe7f5d2e7a080aef3 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.Collections.Generic; | |
using System.Text; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
namespace publish | |
{ | |
public static class WebOperations<Type> | |
{ | |
/// <summary> | |
/// urlに対してcontentをPostする | |
/// </summary> | |
/// <param name="url">postするurl</param> | |
/// <param name="content">postするデータ</param> | |
/// <returns></returns> | |
public static async Task<Type> HttpPost(string url, HttpContent content) | |
{ | |
var httpClient = new HttpClient(); | |
try | |
{ | |
var response = await httpClient.PostAsync(url, content); | |
var json = await response.Content.ReadAsStringAsync(); | |
return ParseJson(json); | |
} | |
catch (HttpRequestException ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// urlに対してGetする | |
/// urlに対してすべてのデータを取得する | |
/// </summary> | |
/// <param name="url">getするurl</param> | |
/// <returns></returns> | |
//public static async Task<Models.ItemModel> HttpGet(string url) | |
public static async Task<Type> HttpGet(string url) | |
{ | |
var httpClient = new HttpClient(); | |
try | |
{ | |
var response = await httpClient.GetAsync(url); | |
var json = await response.Content.ReadAsStringAsync(); | |
return ParseJson(json); | |
} | |
catch (HttpRequestException ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// urlに対してGetする | |
/// keyと一致する値を取得する | |
/// </summary> | |
/// <param name="url">getするurl</param> | |
/// <param name="key">照合するkey値</param> | |
/// <returns></returns> | |
public static async Task<Type> HttpGet(string url, string key) | |
{ | |
var httpClient = new HttpClient(); | |
try | |
{ | |
var response = await httpClient.GetAsync(string.Format("{0}({1})",url, key)); | |
var json = await response.Content.ReadAsStringAsync(); | |
return ParseJson(json); | |
} | |
catch (HttpRequestException ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// urlに対してputする | |
/// </summary> | |
/// <param name="url">putするurl</param> | |
/// <param name="content">putするデータ</param> | |
/// <returns></returns> | |
public static async Task<Type> HttpPut(string url, HttpContent content) | |
{ | |
var httpClient = new HttpClient(); | |
try | |
{ | |
var response = await httpClient.PutAsync(url, content); | |
var json = await response.Content.ReadAsStringAsync(); | |
return ParseJson(json); | |
} | |
catch (HttpRequestException ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// Dictionaryを送信するcontentに変換する | |
/// </summary> | |
/// <param name="dataContent">変換するためのDictionary</param> | |
/// <returns></returns> | |
public static HttpContent CreateHttpContent(Dictionary<string, string> dataContent) | |
{ | |
var content = new FormUrlEncodedContent(dataContent); | |
return content; | |
} | |
private static Type ParseJson(string targetString) | |
{ | |
var parseResult = JsonConvert.DeserializeObject<Type>(targetString); | |
return parseResult; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment