国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

對于System.Net.Http的學(xué)習(xí)(一)——System.Net.Http簡介(轉(zhuǎn))

2019-11-14 13:34:54
字體:
供稿:網(wǎng)友

最新在學(xué)習(xí)System.Net.Http的知識,看到有篇文章寫的十分詳細(xì),就想轉(zhuǎn)過來,自己記錄下。原地址是http://www.survivalescaperooms.com/chillsrc/p/3439215.html?utm_source=tuicool&utm_medium=referral

System.Net.Http 是微軟推出的最新的 HTTP 應(yīng)用程序的編程接口, 微軟稱之為“現(xiàn)代化的 HTTP 編程接口”, 主要提供如下內(nèi)容:

1. 用戶通過 HTTP 使用現(xiàn)代化的 Web Service 的客戶端組件

2. 能夠同時在客戶端與服務(wù)端同時使用的 HTTP 組件(比如處理 HTTP 標(biāo)頭和消息), 為客戶端和服務(wù)端提供一致的編程模型。

 

命名空間 System.Net.Http 以及 System.Net.Http.Headers 提供了如下內(nèi)容:

1. HttpClient 發(fā)送和接收 HTTP 請求與響應(yīng);

2. HttPRequestMessage and HttpResponseMessage 封裝了 RFC 2616 定義的 HTTP 消息;

3. HttpHeaders 封裝了 RFC 2616 定義的 HTTP 標(biāo)頭;

4. HttpClientHandler 負(fù)責(zé)生成HTTP響應(yīng)消息的HTTP處理程序。

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

 

 

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

 

  System.Net.Http 最早是和 asp.net Mvc4 同時出現(xiàn), 是一個第三方組件,名稱是Microsoft HTTP Client Libraries,可以在 .Net 4.0 中使用。 隨著 .Net 4.5 的發(fā)布, System.Net.Http 正式成為 .Net 基礎(chǔ)類庫, 目前已經(jīng)可以在 .Net 4.0/4.5 、 Windows Phone 、 以及 Windows Store App 中使用。 

  HttpClient 組件類實例為一個會話發(fā)送 HTTP 請求。 HttpClient 實例設(shè)置為集合會應(yīng)用于該實例執(zhí)行的所有請求。 此外,每 HttpClient 實例使用自己的連接池,隔離其他 HttpClient實例的執(zhí)行請求。 HttpClient 也是更具體的 HTTP 客戶端的基類。 

  默認(rèn)情況下,使用 HttpWebRequest 向服務(wù)器發(fā)送請求。 這一行為可通過在接受一個HttpMessageHandler實例作為參數(shù)的構(gòu)造函數(shù)重載中指定不同的通道來更改。

  如果需要身份驗證或緩存的功能,WebRequestHandler 可使用配置項和實例傳遞給構(gòu)造函數(shù)。 返回的處理程序傳遞到采用 HttpMessageHandler 參數(shù)的某構(gòu)造進行返回參數(shù)傳遞。 

  如果使用 HttpClient 和相關(guān)組件類的 app 在 System.Net.Http 命名空間用于下載大量數(shù)據(jù) (可達 50 MB 或更多),則應(yīng)用程序應(yīng)這些下載的流和不使用默認(rèn)值緩沖區(qū)。 如果使用默認(rèn)值緩沖區(qū)客戶端內(nèi)存使用量會非常大,可能會導(dǎo)致顯著降低的性能。 

  

  對于 HttpClient的基本使用方法,示例代碼如下:

//聲明HttpClient

HttpClient client = new HttpClient

{

BaseAddress = new Uri("http://www.163.com")

};

//多線程中跨線程進行信息顯示委托

public delegate void ShowMsgDelegate(string text);

public void ShowMsgText(string text)

{

txtMsg.Text=text;

}

//信息顯示

private void ShowMessage(string msg)

{

if (this.InvokeRequired)

{

this.Invoke(new ShowMsgDelegate(ShowMsgText), msg);

}

else

{

ShowMsgText(msg);

}

}

 

// Get form data to server

 

private void btnGet_Click(object sender, EventArgs e)

{

// Get string from server

client.GetStringAsync("browserhttp/").ContinueWith(t =>

{

if (t.IsFaulted)

{

ShowMessage("返回信息錯誤:" + t.Result);

}

else

{

ShowMessage("成功:" + t.Result);

}

});

}

 

 

 

// Post form data to server

 

private void btnPost_Click(object sender, EventArgs e)

{

var param = new Dictionary<string, string> {

{"Name", "TOM Post"},

{"Age", "11"},

{"Birthday", DateTime.Now.ToString("yyyyMMdd")}

};

 

client.PostAsync("browserhttp/", new FormUrlEncodedContent(param)).ContinueWith(t =>

{

ShowMsgDelegate showmsg = new ShowMsgDelegate(ShowMsgText);

if (t.IsFaulted)

{

ShowMessage("返回信息錯誤:" + t.Result);

 

}

else

{

HttpResponseMessage response = t.Result;

ShowMessage(response.StatusCode.ToString());

}

});

 

}

// PUT to update

private void btnPut_Click(object sender, EventArgs e)

{

var param = new Dictionary<string, string> {

{"Id", "10" },

{"Name", "Tom Post"},

{"Age", "10"},

{"Birthday", DateTime.Now.ToString("yyyyMMdd")}

};

client.PutAsync("clienthttp/1", new FormUrlEncodedContent(param)).ContinueWith(t =>

{

if (t.IsFaulted)

{

ShowMessage("返回信息錯誤:" + t.Result);

}

else

{

HttpResponseMessage response = t.Result;

ShowMessage(response.StatusCode.ToString());

}

});

}

 

// DELETE

 

private void btnDel_Click(object sender, EventArgs e)

{

client.DeleteAsync("clienthttp/1").ContinueWith(t =>

{

if (t.IsFaulted)

{

ShowMessage("返回信息錯誤:" + t.Result);

 

}

else

{

HttpResponseMessage response = t.Result;

ShowMessage(response.StatusCode.ToString());

}

});

}

支持職責(zé)鏈模式的 MessageProcessingHandler ,MessageProcessingHandler - 一種基本的 HTTP 消息處理程序。這是最容易進行派生的處理程序,應(yīng)該作為大多數(shù)自定義處理程序的起點。 自已定義了一個新的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;

}

}

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

private void btnCustom_Click(object sender, EventArgs e)

{

var customHandler = new CustomProcessingHandler

{

InnerHandler = new HttpClientHandler()

};

 

var client = new HttpClient(customHandler, true)

{

BaseAddress = new Uri("http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl")

};

var task = client.GetAsync(client.BaseAddress);

task.Result.EnsureSuccessStatusCode();

HttpResponseMessage response = task.Result;

txtStatusCode.Text = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;

txtStatusText.Text = "請求返回結(jié)果如下 ...";

var result = response.Content.ReadAsStringAsync();

txtMsg.Text = result.Result; ;

 

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新昌县| 涿鹿县| 涿州市| 苏州市| 武川县| 金湖县| 神农架林区| 长岭县| 唐山市| 聂拉木县| 宜春市| 桐庐县| 松原市| 涿鹿县| 五寨县| 两当县| 泰州市| 定襄县| 离岛区| 高雄县| 安徽省| 乌拉特中旗| 余江县| 朔州市| 桐城市| 济南市| 东城区| 新龙县| 土默特左旗| 汕尾市| 临夏市| 鄂尔多斯市| 靖江市| 留坝县| 乌兰县| 武穴市| 阳曲县| 黄浦区| 盐源县| 城口县| 万宁市|