帶着問題去思考,你們好!
它是什麼?它包含什麼?它能幹什麼?編程
消息異步
HTTP編程模型的核心就是消息抽象,表示爲:HttPRequestMessage,HttpResponseMessage.用於客戶端和服務端之間交換請求和響應消息。ide
HttpMethod類包含了一組靜態屬性:post
private static readonly HttpMethod getMethod = new HttpMethod("GET"); private static readonly HttpMethod putMethod = new HttpMethod("PUT"); private static readonly HttpMethod postMethod = new HttpMethod("POST"); private static readonly HttpMethod deleteMethod = new HttpMethod("DELETE"); private static readonly HttpMethod headMethod = new HttpMethod("HEAD"); private static readonly HttpMethod optionsMethod = new HttpMethod("OPTIONS"); private static readonly HttpMethod traceMethod = new HttpMethod("TRACE")
標頭spa
消息內容code
HttpContent包含了非虛擬公共方法orm
第一種方式用於推送方式訪問原始的消息內容。將一個流傳遞給CopyAsync方法,而後把消息內容推送到這個流中blog
using(car client=new HtppClient()) { var response= await client.GetAsync("",HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); var ms=new MemorySteam(); await response.Content.CopyToAsync(ms); Assert.True(ms.Length>0); }
也可使用ReadAsStreamAsync().拉取方式訪問。這個方法異步返回一個流字符串
using(var client=new HttpClient()) { var response = await client.GetAsync(""); response.EnsureSuccessStatusCode(); var steam = await response.Content.ReadAsStreamAsync(); var buffer = new byte[2 * 1024]; var len = await steam.ReadAsync(buffer, 0, buffer.Length); }
ReadAsStringAsync和ReadAsByteArrayAsync-異步提供消息內容的緩衝副本。ReadAsStringAsync返回原始的字節內容,ReadAsByteArrayAsync將內容解碼爲字符串返回get
固然也能夠擴展爲
public override Task<object> ReadContentAsync(HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger)