Created
June 28, 2024 07:59
-
-
Save tqk2811/cd78a2e03d6a6f2c49ac0509a2608064 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WrapperStream : Stream | |
{ | |
readonly HttpResponseMessage _httpResponseMessage; | |
readonly Stream _stream; | |
public WrapperStream(HttpResponseMessage httpResponseMessage, Stream stream) | |
{ | |
this._httpResponseMessage = httpResponseMessage; | |
this._stream = stream; | |
} | |
public override bool CanRead => _stream.CanRead; | |
public override bool CanSeek => _stream.CanSeek; | |
public override bool CanWrite => _stream.CanWrite; | |
public override long Length => _stream.Length; | |
public override long Position { get => _stream.Position; set => _stream.Position = value; } | |
public override void Flush() => throw new NotImplementedException(); | |
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException(); | |
public override void SetLength(long value) => throw new NotImplementedException(); | |
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException(); | |
public override int Read(byte[] buffer, int offset, int count) | |
=> _stream.Read(buffer, offset, count); | |
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) | |
=> _stream.BeginRead(buffer, offset, count, callback, state); | |
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) | |
=> _stream.BeginWrite(buffer, offset, count, callback, state); | |
protected override void Dispose(bool disposing) | |
{ | |
_stream.Dispose(); | |
_httpResponseMessage.Dispose(); | |
base.Dispose(disposing); | |
} | |
static HttpClient _client = new HttpClient(); | |
public static async void Test() | |
{ | |
using HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://videoabc.com"); | |
HttpResponseMessage httpResponseMessage = await _client.SendAsync(httpRequestMessage,HttpCompletionOption.ResponseHeadersRead); | |
if(httpResponseMessage.IsSuccessStatusCode) | |
{ | |
Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync(); | |
Response.OutputStream = new WrapperStream(httpResponseMessage, stream); | |
} | |
else | |
{ | |
//báo lỗi | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment