-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpResponse.cs
54 lines (46 loc) · 1.5 KB
/
HttpResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.IO;
using System.Net;
namespace WebLib
{
public class HttpResponse : IDisposable
{
public HttpWebResponse HttpWebResponse { get; }
public bool HaveErrors { get; }
public WebException WebException { get; }
public HttpResponse(HttpWebResponse httpWebResponse) : this(httpWebResponse, false, null)
{
}
public HttpResponse(HttpWebResponse httpWebResponse, bool haveErrors, WebException webException)
{
HttpWebResponse = httpWebResponse;
HaveErrors = haveErrors;
WebException = webException;
}
public WebHeaderCollection Headers => HttpWebResponse.Headers;
public HttpStatusCode StatusCode => HttpWebResponse.StatusCode;
public string GetText()
{
using (Stream stream = GetStream())
using (StreamReader streamReader = new StreamReader(stream))
return streamReader.ReadToEnd();
}
public byte[] GetBytes(int bufferSize = 81920)
{
using (Stream stream = GetStream())
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream, bufferSize);
return memoryStream.ToArray();
}
}
public Stream GetStream()
{
return HttpWebResponse.GetResponseStream();
}
public void Dispose()
{
HttpWebResponse.Dispose();
}
}
}