UrlEncode をキャラクタセットを指定して送れるようになっています。受信データは、Content-Type でキャラクタセットが指定されておれば、自動的に変換されています。
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace winofsql { class Tool { // 戻される文字列は、サーバー側で Content-Type に Charset が // 指定されておれば自動変換されます // ********************************************* // UTF-8 POST // ********************************************* public static async Task<string> Post(string url, Dictionary<string, string> param) { string result = ""; try { HttpClient httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = int.MaxValue; HttpContent content = new FormUrlEncodedContent(param); var response = await httpClient.PostAsync(url, content); String text = await response.Content.ReadAsStringAsync(); result = text; } catch (Exception Err) { result = "ERROR: " + Err.Message; } return result; } // ********************************************* // エンコード指定 POST // ********************************************* public static async Task<string> Post(string url, string encoding, Dictionary<string, string> param) { string result = ""; string query_string = ""; byte[] data1 = null; byte[] data2 = null; string data3 = null; foreach (KeyValuePair<string, string> kvp in param) { if (query_string == "") { query_string += ""; } else { query_string += "&"; } data1 = Encoding.GetEncoding(encoding).GetBytes(kvp.Value); data2 = WebUtility.UrlEncodeToBytes(data1, 0, data1.Length); data3 = Encoding.GetEncoding(encoding).GetString(data2, 0, data2.Length); query_string += kvp.Key + "=" + data3; } try { HttpClient httpClient = new HttpClient(); HttpContent content = new StringContent(query_string); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); var response = await httpClient.PostAsync(url, content); String text = await response.Content.ReadAsStringAsync(); result = text; } catch (Exception Err) { result = "ERROR: " + Err.Message; } return result; } // ********************************************* // URL のみ呼び出し GET // ********************************************* public static async Task<string> Get(string url) { string result = ""; HttpClient httpClient = new HttpClient(); HttpResponseMessage response = null; try { response = await httpClient.GetAsync(url); } catch (Exception Err) { result = "ERROR: " + Err.Message; } // 接続に失敗 if (response == null) { return result; } try { response.EnsureSuccessStatusCode(); } catch (Exception Err) { result = "ERROR: " + Err.Message; } // HTTP 応答の失敗 if (!response.IsSuccessStatusCode) { return result; } // 内容を文字列として取得 try { String text = await response.Content.ReadAsStringAsync(); result = text; } catch (Exception Err) { result = "ERROR: " + Err.Message; } return result; } // ********************************************* // データ呼び出し( UTF-8 ) GET // ********************************************* public static async Task<string> Get(string url, Dictionary<string, string> param) { string query_string = ""; foreach (KeyValuePair<string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } query_string += kvp.Key + "=" + WebUtility.UrlEncode(kvp.Value); } return await Get(url + query_string); } // ********************************************* // データ呼び出し( エンコード指定 ) GET // ********************************************* public static async Task<string> Get(string url, string encoding, Dictionary<string, string> param) { string query_string = ""; byte[] data1 = null; byte[] data2 = null; string data3 = null; foreach (KeyValuePair<string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } data1 = Encoding.GetEncoding(encoding).GetBytes(kvp.Value); data2 = WebUtility.UrlEncodeToBytes(data1, 0, data1.Length); data3 = Encoding.GetEncoding(encoding).GetString(data2, 0, data2.Length); query_string += kvp.Key + "=" + data3; } return await Get(url + query_string); } } }
呼び出し
string result = await winofsql.Tool.Post( "http://localhost/lightbox/sample/test.php", "shift_jis", new Dictionary<string, string>() { { "field1", "日本語" }, { "field2", "表示" } }); if (result.PadRight(5).Substring(0, 5) == "ERROR") { Debug.WriteLine(result); }
関連する記事 Framework4(C#) : WebClient で Post と Get する汎用 static クラス Framework4(C#) : Windows Phone OS 7.1 : WebClient で Post と Get する汎用 static クラス Android で Post と Get
|
【Win8 ストアアプリの最新記事】
- Win8.1 ストアアプリ(JS) : Visual Studio 2013 で Three.js(v65) の WebGLRenderer の動作を確認しました
- WinJS ストア : Three.js を組み込んで、『画像を飛ばす』テンプレート( Bird.js を利用 )
- WinJS ストア : 『背景画像をチェンジする2画面アプリ』のテンプレート
- VS2012ストア(C#) : WebView テンプレート
- VS2012(C#)ストア : ListView Twitter 検索テンプレート
- イラストを背景にして2ページの画面遷移を解りやすくした Windows Store テンプレート
- Twitter API の自分のアプリのトークンを使って投稿するだけの class VS2012_Twitter
- Win8 ストア(C#) / PDF viewer sample (Windows 8.1)
- ストアアプリの TextBox のスクロールバー
- Win8 ストアアプリの、メモリ上にページを残す画面遷移と、前画面のコントロールの参照
- Win8 ストアアプリで、『選択肢を応答するダイアログ』を簡単に使うための MessageBox クラス
- Win8 ストアから Post 投稿
- Win8ストア XAML の AppBarButtonStyle のContent に指定する 16進数 Unicode の取得
- Win8 ストア : UrlEncode と UrlDecode
- Win8 ストア : HttpClient + XDocument で RSS の取得
- Win8 ストア : リストボックス テンプレート
- Win8 ストア : ファイルアクセス テンプレート
- Win8 ストア : ストアブランク テンプレート
- AppBar テンプレート / Win8 ストアアプリ(C#)
- Windows ストアアプリの AppBar を作成してテストする