-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSonRequest.cs
More file actions
138 lines (118 loc) · 4.26 KB
/
Copy pathJSonRequest.cs
File metadata and controls
138 lines (118 loc) · 4.26 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Author: Wayne Gu
// Created: 2016-6-20 14:00
// Project: HttpLibrary.Net
// License: MIT license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using HttpLibrary.Common;
using System.Net;
using System.Diagnostics;
using HttpLibrary.Interop;
namespace HttpLibrary.Http
{
/// <summary>
/// Http request which contains json body
/// </summary>
public class JSonRequest : HttpRequest
{
public JSonRequest(IHttpLibraryPlatform platform, string uri, JObject jObject, string method = HttpConst.HttpMethod_Post)
: base(platform, uri, method)
{
#if DEBUG
Object = jObject;
#endif
DiagnoseHelper.CheckArgument(jObject, "jObject can not be null");
Serialize2Stream(jObject);
}
#if DEBUG
public JObject Object { get; private set; }
#endif
private void Serialize2Stream(JObject jObject)
{
string jsonText = jObject.ToString();
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
stream.Seek(0, SeekOrigin.Begin);
SetRequestStream(stream, HttpLayer.JSonContentType);
}
}
class ContentTypeEntity
{
private static readonly char[] splitor = { ';' };
public ContentTypeEntity(string rawContentType)
{
var cts = rawContentType.Split(splitor, StringSplitOptions.RemoveEmptyEntries);
ContentType = cts[0];
Encoding = Encoding.UTF8;
}
public Encoding Encoding { get; set; }
public string ContentType { get; set; }
}
/// <summary>
/// Http response which contains json body
/// </summary>
public class JSonResponse : HttpResponse
{
public JSonResponse(HttpWebResponse response, int requestId)
: base(response, requestId)
{
//DeseralizeFromStream();
}
/// <summary>
/// Json body
/// </summary>
public Object Object
{
get;
private set;
}
internal override void DeseralizeFromStream()
{
DiagnoseHelper.CheckReference(ResponseStream, "Can not get response stream");
ResponseStream.Seek(0, SeekOrigin.Begin);
ContentTypeEntity ct = new ContentTypeEntity(OriginalResponse.ContentType);
int contentLen = (int)OriginalResponse.ContentLength;
//DiagnoseHelper.CheckStringIgnoreCase(ct.ContentType, HttpLayer.JSonContentType, "Login response content type is not correct");
byte[] buffer = null;
if (contentLen != -1)
{
buffer = new byte[contentLen];
int leftLen = contentLen;
while (leftLen != 0)
{
int readLen = ResponseStream.Read(buffer, 0, leftLen);
leftLen -= readLen;
if (leftLen != 0)
{
//LogHelper.OnlineLogger.Debug("ToDo:: can not read all data in once, need to sleep!" + HttpLayer.LogRequetId(ResponseId));
Debug.WriteLine("ToDo:: can not read all data in once, need to sleep!" + HttpLayer.LogRequetId(ResponseId));
}
}
}
else
{
List<byte> buffList = new List<byte>();
int tempBufLen = 10 * 1024;
byte[] bufTemp = new byte[tempBufLen];
int readLen = 0;
while ((readLen = ResponseStream.Read(bufTemp, 0, tempBufLen)) != 0)
{
if (readLen == tempBufLen)
buffList.AddRange(bufTemp);
else
buffList.AddRange(bufTemp.Take(readLen));
}
buffer = buffList.ToArray();
contentLen = buffList.Count;
}
string sResult = ct.Encoding.GetString(buffer, 0, contentLen);
DiagnoseHelper.CheckString(sResult, "Login response content is empty");
this.Object = JsonConvert.DeserializeObject(sResult);
}
}
}