<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      System.Net.Http for Silverlight

      System.Net.Http 簡介

      System.Net.Http 是微軟推出的最新的 HTTP 應用程序的編程接口, 微軟稱之為“現代化的 HTTP 編程接口”, 旨在提供如下內容:

      1. 用戶通過 HTTP 使用現代化的 Web Service 的客戶端組件;
      2. 能夠同時在客戶端與服務端同時使用的 HTTP 組件(比如處理 HTTP 標頭和消息), 為客戶端和服務端提供一致的編程模型。

      命名空間 System.Net.Http 以及 System.Net.Http.Headers 提供了如下內容:

      1. HttpClient 發送和接收 HTTP 請求與響應;
      2. HttpRequestMessage and HttpResponseMessage 封裝了 RFC 2616 定義的 HTTP 消息;
      3. HttpHeaders 封裝了 RFC 2616 定義的 HTTP 標頭;
      4. HttpClientHandler 負責生成HTTP響應消息的HTTP處理程序。

      System.Net.Http 能夠處理多種類型的 RFC 2616 定義的 HTTP 實體正文, 如下圖所示:

      HttpContent Class Diagrams

      此外, System.Net.Http 對 HTTP 消息的處理采用了職責鏈模式, 這里有一遍不錯的介紹, 這里就不再多說了。

      Silverlight 版本的 System.Net.Http

      System.Net.Http 最早和 Asp.Net Mvc4 同時出現, 可以在 .Net 4.0 中使用。 隨著 .Net 4.5 的發布, System.Net.Http 正式成為 .Net 基礎類庫, 目前已經可以在 .Net 4.0/4.5 、 Windows Phone 、 以及 Windows Store App 中使用, 唯獨沒有發布 Silverlight 版本的 System.Net.Http。 更加悲催的是, 隨著 Xamarin 2.0 的發布, Xamarin.Android 和 Xamarin.iOS 居然也開始支持 System.Net.Http , 真是讓做 Silverlight 開發的碼農心寒。

      幸好, .Net 有開源的實現, 那就是 Mono , 其中有大量開源的 .Net 基礎類實現, 在 Mono 3.x 版本中, 就有開源的 System.Net.Http , Xamarin 發布的 Android 和 iOS 版本的 System.Net.Http 就是源自 Mono 的, 既然 Android 和 iOS 可以, 相信 Silverlight 也肯定可以, 抱著試試看的態度, 下載了 Mono 下的 System.Net.Http 源代碼, 并整理成了一個 Silverlight 項目。 經過一番努力, Silverlight 版本的 System.Net.Http 終于可以使用了, GitHub 項目地址: https://github.com/beginor/System_Net_Http , 歡迎圍觀。

      由于 Silverlight 平臺對 HTTP 的限制, 移除了部分功能, 例如 Proxy 、 AllowAutoRedirect 、 PreAuthenticate 以及 KeepAlive 設置等, 這些都是 Silverlight 不支持的。

      對于 Silverlight 的 BrowserHttp , 僅僅支持 GET 和 POST 方法, 示例代碼如下:

      HttpClient client = new HttpClient {
         BaseAddress = new Uri("http://localhost:8080/HttpTestWeb/api/")
      };
      
      // Get string from server
      client.GetStringAsync("browserhttp/").ContinueWith(t => {
         if (t.IsFaulted) {
            // report error here
            //Application.Current.ReportError(t.Exception.GetBaseException());
         } else {
            string txt = t.Result;
            //Assert.IsFalse(string.IsNullOrEmpty(txt));
         }
      });
      
      // Post form data to server
      var param = new Dictionary<string, string> {
         {"Name", "Client Post"},
         {"Age", "1"},
         {"Birthday", DateTime.Today.ToString("s")}
      };
      client.PostAsync("browserhttp/", new FormUrlEncodedContent(param)).ContinueWith(t => {
         if (t.IsFaulted) {
            // report error here
            // Application.Current.ReportError(t.Exception.GetBaseException());
         } else {
            HttpResponseMessage response = t.Result;
            //Assert.IsTrue(response.EnsureSuccessStatusCode);
         }
      });

      對于 ClientHttp , 除了 GET 和 POST 之外, 還支持 PUT 和 DELETE (其它的 HTTP 方法也可能支持, 未測試), 示例代碼如下:

      // PUT to update
      var param = new Dictionary<string, string> {
         {"Id", "1" },
         {"Name", "Client Post"},
         {"Age", "1"},
         {"Birthday", DateTime.Today.ToString("s")}
      };
      client.PutAsync("clienthttp/1", new FormUrlEncodedContent(param)).ContinueWith(t => {
         if (t.IsFaulted) {
            // report error here
            // Application.Current.ReportError(t.Exception.GetBaseException());
         } else {
            HttpResponseMessage response = t.Result;
            //Assert.IsTrue(response.EnsureSuccessStatusCode);
         }
      });
      
      // DELETE
      client.DeleteAsync("clienthttp/1").ContinueWith(t => {
         if (t.IsFaulted) {
            // report error here
            // Application.Current.ReportError(t.Exception.GetBaseException());
         } else {
            HttpResponseMessage response = t.Result;
            //Assert.IsTrue(response.EnsureSuccessStatusCode);
         }
      });

      支持職責鏈模式的 MessageProcessingHandler , 如下面的代碼所示:

      public class CustomProcessingHandler : MessageProcessingHandler {
      
         protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, CancellationToken cancellationToken) {
            if (request.Method != HttpMethod.Get && request.Method != HttpMethod.Post) {
               request.Headers.TryAddWithoutValidation("RequestMethod", request.Method.Method);
               request.Method = HttpMethod.Post;
            }
            return request;
         }
      
         protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, CancellationToken cancellationToken) {
            var request = response.RequestMessage;
            if (request.Headers.Contains("RequestMethod")) {
               IEnumerable<string> values;
               if (request.Headers.TryGetValues("RequestMethod", out values)) {
                  request.Method = new HttpMethod(values.First());
               }
            }
            return response;
         }
      }

      使用起來也是非常簡單的:

      var customHandler = new CustomProcessingHandler {
          InnerHandler = new HttpClientHandler()
      };
      var client = new HttpClient(customHandler, true) {
          BaseAddress = new Uri("http://localhost:8080/HttpTestWeb/api/")
      };

      參考資料:

      posted @ 2013-04-14 07:09  張志敏  閱讀(3076)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 大同县| 精品亚洲AⅤ无码午夜在线| 人妻丝袜AV中文系列先锋影音| аⅴ天堂国产最新版在线中文| 91一区二区三区蜜桃臀| 亚洲av在线观看| 中文字幕亚洲人妻系列| 欧美性潮喷xxxxx免费视频看| 一卡二卡三卡四卡视频区| 国产亚洲精久久久久久无码77777| 一本色道国产在线观看二区 | 亚洲综合精品中文字幕| 国产精品一区二区三区蜜臀| 在线看av一区二区三区| 成人性无码专区免费视频| 国产一精品一av一免费| 91精品一区二区蜜桃| 东京热人妻无码一区二区av| 四虎永久精品在线视频| 国产精品二区中文字幕| 久久久久国精品产熟女久色 | 五月综合网亚洲乱妇久久| 国产亚洲AV电影院之毛片| 337p粉嫩大胆色噜噜噜| 无码全黄毛片免费看| 婷婷久久香蕉五月综合加勒比| 无码人妻久久久一区二区三区| 免费天堂无码人妻成人av电影| 极品少妇无套内射视频| 两性午夜刺激性视频| 激情久久综合精品久久人妻| 97久久综合亚洲色hezyo| 日日噜噜噜夜夜爽爽狠狠视频| 精品人妻伦一二二区久久| 亚洲第一综合天堂另类专| 亚洲精品一区二区三区大桥未久| 人妻无码久久久久久久久久久 | 亚洲精品~无码抽插| 乱人伦人妻精品一区二区| 国产尤物精品自在拍视频首页| 97人洗澡人人澡人人爽人人模|