2019-07-30 16:47:10 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using NetHttpClient = System.Net.Http.HttpClient;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Storage.Internal {
|
2019-08-07 16:35:21 +08:00
|
|
|
|
public class HttpClient {
|
2019-07-30 16:47:10 +08:00
|
|
|
|
static readonly HashSet<string> HttpContentHeaders = new HashSet<string> {
|
|
|
|
|
|
{ "Allow" },
|
|
|
|
|
|
{ "Content-Disposition" },
|
|
|
|
|
|
{ "Content-Encoding" },
|
|
|
|
|
|
{ "Content-Language" },
|
|
|
|
|
|
{ "Content-Length" },
|
|
|
|
|
|
{ "Content-Location" },
|
|
|
|
|
|
{ "Content-MD5" },
|
|
|
|
|
|
{ "Content-Range" },
|
|
|
|
|
|
{ "Content-Type" },
|
|
|
|
|
|
{ "Expires" },
|
|
|
|
|
|
{ "Last-Modified" }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
readonly NetHttpClient client;
|
|
|
|
|
|
|
|
|
|
|
|
public HttpClient() {
|
|
|
|
|
|
client = new NetHttpClient();
|
2019-08-09 12:27:35 +08:00
|
|
|
|
// 设置版本号
|
|
|
|
|
|
client.DefaultRequestHeaders.Add("User-Agent", $"LeanCloud-csharp-sdk-{AVClient.Version}");
|
2019-07-30 16:47:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public HttpClient(NetHttpClient client) {
|
|
|
|
|
|
this.client = client;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Tuple<HttpStatusCode, string>> ExecuteAsync(HttpRequest httpRequest,
|
|
|
|
|
|
IProgress<AVUploadProgressEventArgs> uploadProgress,
|
|
|
|
|
|
IProgress<AVDownloadProgressEventArgs> downloadProgress,
|
|
|
|
|
|
CancellationToken cancellationToken) {
|
2019-08-07 16:35:21 +08:00
|
|
|
|
|
2019-07-31 15:10:39 +08:00
|
|
|
|
HttpMethod httpMethod = httpRequest.Method;
|
2019-07-30 16:47:10 +08:00
|
|
|
|
HttpRequestMessage message = new HttpRequestMessage(httpMethod, httpRequest.Uri);
|
|
|
|
|
|
|
|
|
|
|
|
// Fill in zero-length data if method is post.
|
2019-07-31 15:10:39 +08:00
|
|
|
|
if (httpRequest.Data == null && httpRequest.Method == HttpMethod.Post) {
|
|
|
|
|
|
message.Content = new StreamContent(new MemoryStream(new byte[0]));
|
2019-07-30 16:47:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-31 15:10:39 +08:00
|
|
|
|
if (httpRequest.Data != null) {
|
|
|
|
|
|
message.Content = new StreamContent(httpRequest.Data);
|
2019-07-30 16:47:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (httpRequest.Headers != null) {
|
|
|
|
|
|
foreach (var header in httpRequest.Headers) {
|
|
|
|
|
|
if (!string.IsNullOrEmpty(header.Value)) {
|
|
|
|
|
|
if (HttpContentHeaders.Contains(header.Key)) {
|
|
|
|
|
|
message.Content.Headers.Add(header.Key, header.Value);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
message.Headers.Add(header.Key, header.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Avoid aggressive caching on Windows Phone 8.1.
|
|
|
|
|
|
message.Headers.Add("Cache-Control", "no-cache");
|
|
|
|
|
|
message.Headers.IfModifiedSince = DateTimeOffset.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
uploadProgress?.Report(new AVUploadProgressEventArgs { Progress = 0 });
|
|
|
|
|
|
var response = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
|
|
|
|
|
uploadProgress?.Report(new AVUploadProgressEventArgs { Progress = 1 });
|
2019-07-31 15:10:39 +08:00
|
|
|
|
message.Dispose();
|
2019-07-30 16:47:10 +08:00
|
|
|
|
|
|
|
|
|
|
var resultString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
response.Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
downloadProgress?.Report(new AVDownloadProgressEventArgs { Progress = 1 });
|
|
|
|
|
|
|
|
|
|
|
|
return new Tuple<HttpStatusCode, string>(response.StatusCode, resultString);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|