Repository navigation
Expand file tree
/
Copy pathResponseViewModel.cs
More file actions
71 lines (57 loc) · 2.27 KB
/
Copy pathResponseViewModel.cs
File metadata and controls
71 lines (57 loc) · 2.27 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
namespace VisualizerExamples.DebuggerVisualizer.ViewModels
{
[Serializable]
public class ResponseViewModel
{
public ResponseViewModel(System.Net.HttpWebResponse httpResponse)
: this((System.Net.WebResponse)httpResponse)
{
StatusCode = (int)httpResponse.StatusCode;
StatusCodeDesc = httpResponse.StatusDescription;
Method = httpResponse.Method;
}
public ResponseViewModel(System.Net.WebResponse response)
{
ContentLength = response.ContentLength;
ContentType = response.ContentType;
ResponseUri = response.ResponseUri.ToString();
RawResponse = ReadResponseStream(response);
}
private string ReadResponseStream(System.Net.WebResponse response)
{
var rawResponse = "";
var stream = response.GetResponseStream();
if (stream != null)
{
// save position before reading
long position = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
using (var readStream = new StreamReader(stream: stream,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: true,
bufferSize: 1024,
leaveOpen: true))
{
rawResponse = readStream.ReadToEnd();
}
// reset seek position so we don't have side effects on the app
stream.Seek(position, SeekOrigin.Begin);
}
return rawResponse;
}
public long ContentLength { get; }
public string ContentType { get; }
public string ResponseUri { get; }
public string RawResponse { get; }
public string StatusCodeDesc { get; }
public int StatusCode { get; }
public string Method { get; }
}
}