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
}
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
}
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"
}
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();
}
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...
}