日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

WebAPI-HTTP編程模型

 精品唯居 2021-04-12

帶著問(wèn)題去思考,大家好!
它是什么?它包含什么?它能干什么?

消息

HTTP編程模型的核心就是消息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務(wù)端之間交換請(qǐng)求和響應(yīng)消息。

HttpMethod類包含了一組靜態(tài)屬性:

   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")

標(biāo)頭

  • HttpRequestHeaders:包含請(qǐng)求標(biāo)頭
  • HttpResponseHeaders:包含響應(yīng)標(biāo)頭
  • HttpContentHeaders:包含內(nèi)容標(biāo)頭

 

消息內(nèi)容

HttpContent包含了非虛擬公共方法

  • Task<string> ReadAsStringAsync()
  • Task<byte[]> ReadAsByteArrayAsync()
  • Task<Stream> ReadAsStreamAsync()
  • Task CopyToAsync(Stream stream, TransportContext context)

第一種方式用于推送方式訪問(wèn)原始的消息內(nèi)容。將一個(gè)流傳遞給CopyAsync方法,然后把消息內(nèi)容推送到這個(gè)流中

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().拉取方式訪問(wèn)。這個(gè)方法異步返回一個(gè)流

            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-異步提供消息內(nèi)容的緩沖副本。ReadAsStringAsync返回原始的字節(jié)內(nèi)容,ReadAsByteArrayAsync將內(nèi)容解碼為字符串返回

當(dāng)然也可以擴(kuò)展為

public override Task<object> ReadContentAsync(HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger)

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多