public void ReadAsStringAsync() { byte[] b = { 77, 55 }; var sc = new ByteArrayContent(b); var res = sc.ReadAsStringAsync().Result; Assert.AreEqual("M7", res, "#1"); }
public static void UsingByteArrayContent() { // ByteArrayContent is hardly different than StringContent, // but it can be more convenient for lower-level operations // when bytes are already available (as opposed to using a Stream). var bytes = Encoding.ASCII.GetBytes(Helpers.__); var content = new ByteArrayContent(bytes); // This also takes offset and limit parameters. var body = content.ReadAsStringAsync().Result; Helpers.AssertEquality("Hello, bytes!", body); }
public async Task Given_content_when_cloning_should_return_byte_array_content_with_same_data_and_headers(HttpContent content, string expectedData) { // Act ByteArrayContent clone = await content.CloneAsByteArrayContentAsync(); // Assert clone.Should().NotBeNull(); clone.Headers.Should().BeEquivalentTo(content.Headers); string actual = await clone.ReadAsStringAsync(); actual.Should().Be(expectedData); }
public async Task ByteArrayContent_can_be_used_to_represent_byte_sequences() { var alreadyExistantArray = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; var content = new ByteArrayContent(alreadyExistantArray); content.Headers.ContentType = new MediaTypeHeaderValue("text/plain") { CharSet = "utf-8" }; var readText = await content.ReadAsStringAsync(); Assert.Equal("Hello", readText); }
private void UpYunAuth(ByteArrayContent requestContent, string method, string uri) { var dt = DateTime.UtcNow; var date = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", new CultureInfo("en-US")); requestContent.Headers.Add("Date", date); var body = requestContent.ReadAsStringAsync().Result; var auth = !string.IsNullOrEmpty(body) ? Md5(method + '&' + uri + '&' + date + '&' + requestContent.ReadAsByteArrayAsync().Result.Length + '&' + Md5(_password)) : Md5(method + '&' + uri + '&' + date + '&' + 0 + '&' + Md5(_password)); requestContent.Headers.Add("Authorization", "UpYun " + _username + ':' + auth); }
public async Task SendAsync_RequestContentNotDisposed() { var content = new ByteArrayContent(new byte[1]); using (var request = new HttpRequestMessage(HttpMethod.Get, CreateFakeUri()) { Content = content }) using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage())))) { await client.SendAsync(request); await content.ReadAsStringAsync(); // no exception } }
public async Task Given_content_when_cloning_multiple_times_should_not_throw(HttpContent content, string expectedData) { // Act for (int i = 0; i < 3; i++) { ByteArrayContent clone = null; Func <Task> act = async() => clone = await content.CloneAsByteArrayContentAsync(); // Assert await act.Should().NotThrowAsync(); clone.Should().NotBeNull(); clone.Headers.Should().BeEquivalentTo(content.Headers, "iteration {0} should have same headers", i); string actual = await clone.ReadAsStringAsync(); actual.Should().Be(expectedData, "iteration {0} should have same data", i); } }
private void upyunAuth(ByteArrayContent requestContent, string method, string uri) { DateTime dt = DateTime.UtcNow; string date = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", new CultureInfo("en-US")); requestContent.Headers.Add("Date", date); string body = requestContent.ReadAsStringAsync().Result; string auth; if (!string.IsNullOrEmpty(body)) { auth = md5(method + '&' + uri + '&' + date + '&' + requestContent.ReadAsByteArrayAsync().Result.Length + '&' + md5(this.password)); } else { auth = md5(method + '&' + uri + '&' + date + '&' + 0 + '&' + md5(this.password)); } requestContent.Headers.Add("Authorization", "UpYun " + this.username + ':' + auth); }
/// <summary> /// 签名认证 /// </summary> /// <param name="requestContent"></param> /// <param name="method"></param> /// <param name="uri"></param> private void UpyunAuth(ByteArrayContent requestContent, string method, string uri) { DateTime dt = DateTime.UtcNow; string date = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", new CultureInfo("en-US")); httpClient.DefaultRequestHeaders.Date = dt; string body = requestContent.ReadAsStringAsync().Result; string auth; if (!string.IsNullOrEmpty(body)) { auth = md5(method + '&' + uri + '&' + date + '&' + requestContent.ReadAsByteArrayAsync().Result.Length + '&' + md5(this.password)); } else { auth = md5(method + '&' + uri + '&' + date + '&' + 0 + '&' + md5(this.password)); } httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("UPYUN", this.username + ':' + auth); }
private async Task UpYunAuthAsync(ByteArrayContent requestContent, string method, string uri, CancellationToken cancellationToken = default) { DateTime dt = DateTime.UtcNow; string date = dt.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", new CultureInfo("en-US")); requestContent.Headers.Add("Date", date); string body = await requestContent.ReadAsStringAsync(); string auth; if (!string.IsNullOrEmpty(body)) { byte[] bytesContent = await requestContent.ReadAsByteArrayAsync(); auth = md5(method + '&' + uri + '&' + date + '&' + bytesContent.Length + '&' + md5(this.password)); } else { auth = md5(method + '&' + uri + '&' + date + '&' + 0 + '&' + md5(this.password)); } requestContent.Headers.Add("Authorization", "UpYun " + this.username + ':' + auth); }