WinRT的 HttpClient

Windows.Web.Http.HttpClient应该是最常用的http通信类了:

public sealed class HttpClient : IDisposable, IStringable {
public HttpClient(); // Construct using HttpBaseProtocolFilter
public HttpClient(IHttpFilter filter); // Construct with a specific IHttpFilter
// Use this property to set request headers sent with each request.
public HttpRequestHeaderCollection DefaultRequestHeaders { get; }
// This method sends a request. The operation is considered complete
// after reading response headers or the entire response.
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
SendRequestAsync(HttpRequestMessage request);
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption);
// These methods simplify calling SendRequestAsync for common operations:
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
PostAsync(Uri uri, IHttpContent content);
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
PutAsync(Uri uri, IHttpContent content);
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> DeleteAsync(Uri uri);
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> GetAsync(Uri uri);
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
GetAsync(Uri uri, HttpCompletionOption completionOption);
// These methods simplify calling GetAsync for common HTP GET operations:
public IAsyncOperationWithProgress<String, HttpProgress> GetStringAsync(Uri uri);
public IAsyncOperationWithProgress<IBuffer, HttpProgress> GetBufferAsync(Uri uri);
public IAsyncOperationWithProgress<IInputStream, HttpProgress> GetInputStreamAsync(Uri uri);
public String ToString(); // Returns filter and default header information
public void Dispose(); // Releases unmanaged resources associated with the HttpClient
}

要控制更多,就要构造一个HttpRequestMessage对象:

public sealed class HttpRequestMessage : IDisposable, IStringable {
// Set the HTP method (GET, POST, PUT, DELETE, etc.) and server URI
public HttpRequestMessage(HttpMethod method, Uri uri);
// Set this request's headers (HttpClient's DefaultRequestHeaders are merged with these)
public HttpRequestHeaderCollection Headers { get; }
// Set any content to send to the server (not used for some methods, like GET)
public IHttpContent Content { get; set; }
// Optional: When using HTPS, examine information about the server's certificate
public HttpTransportInformation TransportInformation { get; }
public void Dispose(); // Releases HttpRequestMessage’s unmanaged resources
}

这是一个Post的例子:

using (HttpClient client = new HttpClient()) {
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Post, new Uri("https://httpbin.org/post")) {
Content = new HttpStringContent("Some test data", UnicodeEncoding.Utf8, "text/plain")
};
HttpResponseMessage response = await client.SendRequestAsync(request);
String json = await response.Content.ReadAsStringAsync();
}

返回:

{
"headers": {
"Content-Length": "14",
"Content-Type": "text/plain; charset=UTF-8",
"Accept-Encoding": "gzip, deflate",
"Cache-Control": "no-cache",
"Connection": "close",
"Host": "httpbin.org"
},
"args": {},
"data": "Some test data",
"files": {},
"url": "http://httpbin.org/post",
"json": null,
"form": {},
"origin": "50.135.158.248"
}

前面用到了HttpStringContent类:

public interface IHttpContent : IDisposable {
HttpContentHeaderCollection Headers { get; } // Use this to set/get content headers
Boolean TryComputeLength(out UInt64 length); // Gets content length in bytes
// Sends content to an output stream
IAsyncOperationWithProgress<UInt64, UInt64> WriteToStreamAsync(IOutputStream outputStream);
// Serializes content into memory
IAsyncOperationWithProgress<UInt64, UInt64> BufferAllAsync();
// Read response content as an IInputStream, IBuffer, or a String
IAsyncOperationWithProgress<IInputStream, UInt64> ReadAsInputStreamAsync();
IAsyncOperationWithProgress<IBuffer, UInt64> ReadAsBufferAsync();
IAsyncOperationWithProgress<String, UInt64> ReadAsStringAsync();
}

还有几个实现了IHttpContent的类:

  • HttpStringContent
  • HttpBufferContent
  • HttpStreamContent
  • HttpFormUrlEncodedContent
  • HttpMultipartContent
  • HttpMultipartFormDataContent
SendRequestAsync返回一个HttpReseponMessage:
public sealed class HttpResponseMessage : IDisposable, IStringable {
// Members that return request's response status
public HttpStatusCode StatusCode { get; set; } // Gets response status code
public Boolean IsSuccessStatusCode { get; } // True for codes between 200-299
public HttpResponseMessage EnsureSuccessStatusCode(); // Throws if !IsSuccessStatusCode
public String ReasonPhrase { get; set; } // Gets status reason (eg: "OK")
public HttpResponseMessageSource Source { get; set; } // Network or Cache
public HttpResponseHeaderCollection Headers { get; } // Returns this response's headers
public IHttpContent Content { get; set; } // Returns content sent from server
public void Dispose(); // Releases HttpResponseMessage’s unmanaged resources
// Some members not shown here...
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值