{"id":3395,"date":"2012-05-08T09:10:08","date_gmt":"2012-05-08T00:10:08","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/3395"},"modified":"2012-05-12T06:22:54","modified_gmt":"2012-05-11T21:22:54","slug":"win8-metro-desktop%e3%81%ae%e3%83%97%e3%83%ad%e3%82%bb%e3%82%b9%e9%96%93%e9%80%9a%e4%bf%a1%e3%82%92web-api%e9%a2%a8%e3%81%ab%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/3395","title":{"rendered":"[win8] metro-desktop\u306e\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3092Web API\u98a8\u306b\u3059\u308b"},"content":{"rendered":"<p>[win8] Metro\u30a2\u30d7\u30ea\u304b\u3089Desktop\u30a2\u30d7\u30ea\u3078WCF\u3067\u63a5\u7d9a\u3059\u308b | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3387\">http:\/\/www.moonmile.net\/blog\/archives\/3387<\/a><br \/>\n[win8] metro \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304b\u3089\u30c7\u30b9\u30af\u30c8\u30c3\u30d7\u30a2\u30d7\u30ea\u306b\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3059\u308b | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3379\">http:\/\/www.moonmile.net\/blog\/archives\/3379<\/a><\/p>\n<p># \u8ffd\u8a18 2012\/05\/12<br \/>\n# \u518d\u5ea6\u78ba\u8a8d\u3057\u305f\u3068\u3053\u308d\u3001localhost  \u306b\u3088\u308b\u30eb\u30fc\u30d7\u30d0\u30c3\u30af\u63a5\u7d9a\u306f\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f5c\u3063\u305f\u6642\u306f\u99c4\u76ee\u3067\u3001Visual Studio  \u304b\u3089\u30c7\u30d0\u30c3\u30b0\u5b9f\u884c\u3057\u305f\u3060\u3051\u63a5\u7d9a\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u3042\u305f\u308a\u3001hosts  \u66f8\u304d\u63db\u3048\u3001ip  \u6307\u5b9a\u3067\u3082\u99c4\u76ee\u306a\u306e\u3067\u3001\u5225\u306e\u65b9\u5f0f\u3092\u8003\u3048\u306a\u3044\u3068\u3002\u4ee5\u4e0b\u306f\u3001\u53c2\u8003\u306e\u305f\u3081\u6b8b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\n# \u696d\u52d9\u7684\u306b\u306f\u3001\u5225\u30de\u30b7\u30f3\u306b proxy \u3092\u7acb\u3066\u3066 localhost-&gt;proxy-&gt;localohst \u306b\u3059\u308c\u3070 ok \u306a\u3093\u3067\u3059\u304c\u3001\u3082\u3046\u3061\u3087\u3063\u3068\u3046\u307e\u3044\u65b9\u6cd5\u3092\u8003\u3048\u307e\u3059\u304b\u3002\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u8ca0\u8377\u304c\u304b\u304b\u308b\u3057\u3002<\/p>\n<p>\u306a\u3068\u3053\u308d\u3067\u3001WCF \u3092\u4f7f\u3063\u3066\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3067\u304d\u308b\u3053\u3068\u306f\u78ba\u8a8d\u3067\u304d\u305f\u306e\u3067\u3059\u304c\u3001WCF \u3060\u3068 metro \u30a2\u30d7\u30ea\u306e\u307b\u3046\u3067 web \u306e\u53c2\u7167\u8a2d\u5b9a\u3092\u3057\u306a\u3044\u3068\u3044\u3051\u307e\u305b\u3093\u3002\u307e\u3041\u3001\u88fd\u54c1\u7684\u306b\u30b5\u30fc\u30d0\u30fc\u304c\u5148\u306b\u56fa\u5b9a\u5316\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u6d41\u52d5\u7684\u306b\u4f5c\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u5148\u306b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u6c7a\u3081\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3068\u3044\u3046\u306e\u306f\u3061\u3087\u3063\u3068\u9177\u3067\u3059\u3002<\/p>\n<p>\u306a\u8a33\u3067\u3001Web API \u98a8\u306b GET \u30b3\u30de\u30f3\u30c9\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u4f7f\u3063\u3066 metro \u30a2\u30d7\u30ea\uff08\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\uff09\u304b\u3089 desktop \u30a2\u30d7\u30ea\uff08\u30b5\u30fc\u30d0\u30fc\uff09\u3078\u9001\u4fe1\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u25a0\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e metro \u30a2\u30d7\u30ea\u5074<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate async void Button_Click_1(object sender, RoutedEventArgs e)\r\n{\r\n    string text = textBox1.Text;\r\n    string url = &quot;http:\/\/localhost:8083\/metro\/method&quot;;\r\n\r\n    HttpClient client = new HttpClient();\r\n    HttpResponseMessage res = await client.GetAsync(url + &quot;\/&quot; + textBox1.Text);\r\n    string response = await res.Content.ReadAsStringAsync();\r\n    textBox2.Text = response;\r\n}\r\n<\/pre>\n<p>metro \u30a2\u30d7\u30ea\u306e\u307b\u3046\u3067\u306f\u3001\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u5b9a\u3057\u3066\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\u3057\u307e\u3059\u3002Web API \u306e REST \u306e\u3088\u3046\u306b\u300chttp:\/\/localhost:8083\/metro\/method\/masuda\/1000\u300d\u3068\u304b\u300chttp:\/\/localhost:8083\/metro\/method?name=masuda&amp;num=1000\u300d\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3059\u3053\u3068\u3092\u60f3\u5b9a\u3057\u307e\u3059\u3002\u3053\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u9069\u5f53\u306b\u547c\u51faURL\u3092\u4f5c\u3063\u3066\u3044\u308b\u3060\u3051\u306a\u306e\u3067\u3001URL\u3092\u4f5c\u308b\u305f\u3081\u306e\u9069\u5f53\u306a\u30e9\u30c3\u30d1\u3092\u4f5c\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u5fdc\u7b54\u306f\u3001XML \u5f62\u5f0f\u3067\u3082\u3088\u3044\u306e\u3067\u3059\u304c\u3001\u81ea\u7531\u306b\u3002<\/p>\n<p>\u25a0Web API\u3092\u63d0\u4f9b\u3059\u308b desktop \u30a2\u30d7\u30ea<\/p>\n<p>HttpListener.BeginGetContext \u30e1\u30bd\u30c3\u30c9 (System.Net)<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/ja-jp\/library\/system.net.httplistener.begingetcontext(v=vs.110).aspx\">http:\/\/msdn.microsoft.com\/ja-jp\/library\/system.net.httplistener.begingetcontext(v=vs.110).aspx<\/a><\/p>\n<p>\u3092\u53c2\u8003\u306b\u3057\u3066\u3001HttpListener \u30af\u30e9\u30b9\u3067\u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u308a\u307e\u3059\u3002\u524d\u56de\u306f\u3001\u540c\u671f\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u305f\u305f\u3081\u306b backgroundWorker \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u30b9\u30ec\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u975e\u540c\u671f\u30e1\u30bd\u30c3\u30c9\u306e BeginGetContext\u3001EndGetContext \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3059\u3002\u4e0a\u8a18\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3067\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u304c static \u306b\u306a\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u666e\u901a\u306e\u5185\u90e8\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3064\u30fc\u304b\u3001\u5185\u90e8\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u305f\u307b\u3046\u304c\u3001ListBox \u306a\u3069\u306e GUI \u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u304b\u3068\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System.Net;\r\nusing System.IO;\r\n\r\nnamespace SampleLocalWebApiServer\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        HttpListener listener;\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            string url = &quot;http:\/\/*:8083\/metro\/&quot;;\r\n            \/\/ \u958b\u59cb\r\n            listener = new HttpListener();\r\n            listener.Prefixes.Add(url);\r\n\r\n            listener.Start();\r\n            listBox1.Items.Add(&quot;\u30b5\u30fc\u30d0\u30fc\u958b\u59cb&quot;);\r\n            listener.BeginGetContext(ListenerCallback, listener);\r\n        }\r\n\r\n        public void ListenerCallback(IAsyncResult result)\r\n        {\r\n            \/\/ HttpListener listener = (HttpListener)result.AsyncState;\r\n            HttpListenerContext context;\r\n            try\r\n            {\r\n                context = listener.EndGetContext(result);\r\n            }\r\n            catch\r\n            {\r\n                \/\/ stop \u30e1\u30bd\u30c3\u30c9\u3067\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3001\u5bfe\u51e6\r\n                return;\r\n            }\r\n            \/\/ var content = listener.GetContext();\r\n            var req = context.Request;\r\n            var url = req.RawUrl;\r\n            var res = context.Response;\r\n\r\n            listBox1.Items.Add(&quot;\u53d7\u4fe1&quot;);\r\n\r\n            var output = new StreamWriter( res.OutputStream ) ;\r\n            output.WriteLine(string.Format(&quot;called {0}&quot;, url));\r\n            output.Close();\r\n\r\n            \/\/ \u6b21\u306e\u53d7\u4fe1\u306e\u6e96\u5099\r\n            listener.BeginGetContext(ListenerCallback, listener);\r\n        }\r\n\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            \/\/ \u7d42\u4e86\r\n            listener.Stop();\r\n            listBox1.Items.Add(&quot;\u30b5\u30fc\u30d0\u30fc\u7d42\u4e86&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u306e\u505c\u6b62\u306a\u306e\u3067\u3059\u304c\u3001Stop \u304b\u3001Abort \u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002\u304c\u3001\u3061\u3087\u3063\u3068\u9762\u5012\u306a\u306e\u306f\u3001Stop \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u305f\u9014\u7aef\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\u3093\u3067\u3059\u3088\u306d&#8230;\u3067\u3001EndGetContext \u30e1\u30bd\u30c3\u30c9\u306e\u547c\u3073\u51fa\u3057\u6642\u306b\u4f8b\u5916\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u30b3\u30fc\u30c9\u3067\u306f try-catch \u3067\u3001\u56de\u907f\u3057\u3066\u3044\u307e\u3059\u3002\u7570\u5e38\u7d42\u4e86\u306e\u5834\u5408\u3068\u533a\u5225\u304c\u3064\u304b\u306a\u3044\u306e\u3067\u3001\u3082\u3046\u3061\u3087\u3063\u3068\u306a\u3093\u3068\u304b\u3057\u305f\u3044\u3068\u3053\u308d\u3067\u3059\u306d\u3002<\/p>\n<p>\u547c\u3073\u51fa\u3057\u305f URL \u306f\u3001RawUrl \u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002URL \u30d7\u30ed\u30d1\u30c6\u30a3\u3067\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001RawURL \u306e\u307b\u3046\u304c\u3001\u30b5\u30fc\u30d0\u30fc\u540d\u3068\u30dd\u30fc\u30c8\u540d\u3092\u524a\u3063\u3066\u304f\u308c\u308b\u306e\u3067\u3001\u51e6\u7406\u304c\u697d\u306a\u306e\u3067\u3059\uff08\u540d\u524d\u304c\u9006\u3063\u307d\u3044\u306e\u306f\u3001\u898b\u306a\u3044\u3053\u3068\u306b\u3057\u3088\u3046\uff09\u3002<\/p>\n<p>\u25a0\u5b9f\u884c\u3057\u3066\u307f\u308b<\/p>\n<p>\u30b5\u30fc\u30d0\u30fc\u3092\u7ba1\u7406\u8005\u6a29\u9650\u3067\u52d5\u304b\u3057\u3066\u3001metro app \u304b\u3089\u3064\u306a\u3052\u3066\u307f\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120507_01org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2012\/05\/wpid-dworkblogimage20120507_01thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>\u7121\u4e8b\u63a5\u7d9a\u3067\u304d\u3066\u3044\u307e\u3059\u306d\u3002\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u9069\u5f53\u306b\u632f\u308a\u5206\u3051\u3066\u3084\u308c\u3070\u3001Web API \u3068\u3057\u3066\u52d5\u304b\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002URL Encode\/Decode\u3059\u308c\u3070\u6587\u5b57\u5217\u306f\u7c21\u5358\u3067\u3059\u3002\u8907\u96d1\u306a\u5834\u5408\u306f POST \u3067\u9001\u3063\u3066\u3001\u9069\u5f53\u306a\u30af\u30e9\u30b9\u3067\u30e9\u30c3\u30d7\u3059\u308c\u3070\u3088\u3044\u3067\u3059\u304b\u306d\u3002\u3068\u3044\u3046\u304b\u3001\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u6a5f\u80fd\u3092\u4f7f\u3048\u3070 ok \u304b\u3082\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[win8] Metro\u30a2\u30d7\u30ea\u304b\u3089Desktop\u30a2\u30d7\u30ea\u3078WCF\u3067\u63a5\u7d9a\u3059\u308b | Moonmile Solutions Blog http:\/\/www.moonmile.net\/blog\/archives\/3387 [win &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/3395\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23,44],"tags":[],"class_list":["post-3395","post","type-post","status-publish","format-standard","hentry","category-csharp","category-windows-8"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=3395"}],"version-history":[{"count":3,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3395\/revisions"}],"predecessor-version":[{"id":3408,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/3395\/revisions\/3408"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=3395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=3395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=3395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}