拡張子:txtvbswsfjsphpjavahtmlutf8sjis using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace LBOX_Http { public class Lbox { 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; } 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 { // HTTP 応答が成功しなかった例外をスローします。 // Content が nullでない場合、このメソッドは、マネージとアンマネージ リソースを解放するために // Dispose を呼び出します。 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; } } } GET呼び出しのサンプルコード 拡張子:txtvbswsfjsphpjavahtmlutf8sjis // 内容を文字列として取得 var string_xml = await Lbox.Get("http://matome.naver.jp/feed/hot"); if (string_xml.Substring(0, 5) == "ERROR") { Debug.WriteLine(string_xml.Substring(5)); return; } // XDocument を作成 XDocument dom = XDocument.Parse(string_xml); // 参照用の名前空間を作成 XNamespace xn_dc = "http://purl.org/dc/elements/1.1/"; XNamespace xn_media = "http://search.yahoo.com/mrss/"; var items = dom.Descendants("item"); foreach (var item in items) { App.listMain.Items.Add(new ListItem2() { id = item.Element(xn_dc + "creator").Value, name = item.Element("title").Value, link = item.Element("link").Value, img = item.Element(xn_media + "content").Attribute("url").Value }); } Win8 ストアから Post 投稿 Win8 ストア : UrlEncode と UrlDecode
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace LBOX_Http { public class Lbox { 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; } 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 { // HTTP 応答が成功しなかった例外をスローします。 // Content が nullでない場合、このメソッドは、マネージとアンマネージ リソースを解放するために // Dispose を呼び出します。 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; } } }
// 内容を文字列として取得 var string_xml = await Lbox.Get("http://matome.naver.jp/feed/hot"); if (string_xml.Substring(0, 5) == "ERROR") { Debug.WriteLine(string_xml.Substring(5)); return; } // XDocument を作成 XDocument dom = XDocument.Parse(string_xml); // 参照用の名前空間を作成 XNamespace xn_dc = "http://purl.org/dc/elements/1.1/"; XNamespace xn_media = "http://search.yahoo.com/mrss/"; var items = dom.Descendants("item"); foreach (var item in items) { App.listMain.Items.Add(new ListItem2() { id = item.Element(xn_dc + "creator").Value, name = item.Element("title").Value, link = item.Element("link").Value, img = item.Element(xn_media + "content").Attribute("url").Value }); }