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缂栫▼妯″瀷的主要内容,如果未能解决你的问题,请参考以下文章