WebAPI-HTTP缂栫▼妯″瀷

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebAPI-HTTP缂栫▼妯″瀷相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ppc' title='ppc'>ppc   option   copyto   浜ゆ崲   contex   ide   assert   for   

甯︾潃闂鍘绘€濊€冿紝澶у濂斤紒
瀹冩槸浠€涔堬紵瀹冨寘鍚粈涔堬紵瀹冭兘骞蹭粈涔堬紵

娑堟伅

HTTP缂栫▼妯″瀷鐨勬牳蹇冨氨鏄秷鎭娊璞★紝琛ㄧず涓猴細HttPRequestMessage,HttpResponseMessage.鐢ㄤ簬瀹㈡埛绔拰鏈嶅姟绔箣闂翠氦鎹㈣姹傚拰鍝嶅簲娑堟伅銆?/p>

HttpMethod绫诲寘鍚簡涓€缁勯潤鎬佸睘鎬э細

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

鏍囧ご

  • HttpRequestHeaders锛氬寘鍚姹傛爣澶?/li>
  • HttpResponseHeaders锛氬寘鍚搷搴旀爣澶?/li>
  • HttpContentHeaders锛氬寘鍚唴瀹规爣澶?/li>

 

娑堟伅鍐呭

HttpContent鍖呭惈浜嗛潪铏氭嫙鍏叡鏂规硶

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

绗竴绉嶆柟寮忕敤浜庢帹閫佹柟寮忚闂師濮嬬殑娑堟伅鍐呭銆傚皢涓€涓祦浼犻€掔粰CopyAsync鏂规硶锛岀劧鍚庢妸娑堟伅鍐呭鎺ㄩ€佸埌杩欎釜娴佷腑

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);
}

涔熷彲浠ヤ娇鐢≧eadAsStreamAsync().鎷夊彇鏂瑰紡璁块棶銆傝繖涓柟娉曞紓姝ヨ繑鍥炰竴涓祦

            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鍜孯eadAsByteArrayAsync-寮傛鎻愪緵娑堟伅鍐呭鐨勭紦鍐插壇鏈€俁eadAsStringAsync杩斿洖鍘熷鐨勫瓧鑺傚唴瀹癸紝ReadAsByteArrayAsync灏嗗唴瀹硅В鐮佷负瀛楃涓茶繑鍥?/p>

褰撶劧涔熷彲浠ユ墿灞曚负

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

 

以上是关于WebAPI-HTTP缂栫▼妯″瀷的主要内容,如果未能解决你的问题,请参考以下文章

甯歌璁捐妯″瀷

鐢⊿park-Scala璁粌LightGBM妯″瀷

Java骞跺彂缂栫▼鐨勮壓鏈紙浜旓級Java鍐呭瓨妯″瀷

鍙鐢ㄦ€т笌璁捐妯″紡

Struts2 杩炴帴鏁版嵁搴撴€荤粨

鐩掓ā鍨嬶紙澶栬竟璺濓級