13
13
using Segment . Exception ;
14
14
using Segment . Model ;
15
15
using Segment . Stats ;
16
+ using System . IO ;
17
+ using System . IO . Compression ;
16
18
17
19
namespace Segment . Request
18
20
{
@@ -83,23 +85,28 @@ internal BlockingRequestHandler(Client client, TimeSpan timeout)
83
85
this . _client = client ;
84
86
this . Timeout = timeout ;
85
87
88
+ // Create HttpClient instance in .Net 3.5
86
89
#if NET35
87
90
_httpClient = new HttpClient { Timeout = Timeout } ;
88
- // set proxy
89
- if ( ! string . IsNullOrEmpty ( _client . Config . Proxy ) )
90
- _httpClient . Proxy = new WebProxy ( _client . Config . Proxy ) ;
91
91
#else
92
+ var handler = new HttpClientHandler ( ) ;
93
+ #endif
94
+
95
+ // Set proxy information
92
96
if ( ! string . IsNullOrEmpty ( _client . Config . Proxy ) )
93
97
{
94
- var handler = new HttpClientHandler
95
- {
96
- Proxy = new WebProxy ( _client . Config . Proxy ) ,
97
- UseProxy = true
98
- } ;
99
- _httpClient = new HttpClient ( handler ) { Timeout = Timeout } ;
98
+ #if NET35
99
+ _httpClient . Proxy = new WebProxy ( _client . Config . Proxy ) ;
100
+ #else
101
+ handler . Proxy = new WebProxy ( _client . Config . Proxy ) ;
102
+ handler . UseProxy = true ;
103
+ #endif
100
104
}
101
- else
102
- _httpClient = new HttpClient ( ) { Timeout = Timeout } ;
105
+
106
+ // Initialize HttpClient instance with given configuration
107
+ #if NET35
108
+ #else
109
+ _httpClient = new HttpClient ( handler ) { Timeout = Timeout } ;
103
110
#endif
104
111
}
105
112
@@ -135,6 +142,29 @@ public async Task MakeRequest(Batch batch)
135
142
_httpClient . DefaultRequestHeaders . Add ( "User-Agent" , szUserAgent ) ;
136
143
#endif
137
144
145
+ // Prepare request data;
146
+ var requestData = Encoding . UTF8 . GetBytes ( json ) ;
147
+
148
+ // Compress request data if compression is set
149
+ if ( _client . Config . CompressRequest )
150
+ {
151
+ #if NET35
152
+ _httpClient . Headers . Add ( HttpRequestHeader . ContentEncoding , "gzip" ) ;
153
+ #else
154
+ //_httpClient.DefaultRequestHeaders.Add("Content-Encoding", "gzip");
155
+ #endif
156
+
157
+ // Compress request data with GZip
158
+ using ( MemoryStream memory = new MemoryStream ( ) )
159
+ {
160
+ using ( GZipStream gzip = new GZipStream ( memory , CompressionMode . Compress , true ) )
161
+ {
162
+ gzip . Write ( requestData , 0 , requestData . Length ) ;
163
+ }
164
+ requestData = memory . ToArray ( ) ;
165
+ }
166
+ }
167
+
138
168
Logger . Info ( "Sending analytics request to Segment.io .." , new Dict
139
169
{
140
170
{ "batch id" , batch . MessageId } ,
@@ -156,7 +186,7 @@ public async Task MakeRequest(Batch batch)
156
186
157
187
try
158
188
{
159
- var response = Encoding . UTF8 . GetString ( _httpClient . UploadData ( uri , "POST" , Encoding . UTF8 . GetBytes ( json ) ) ) ;
189
+ var response = Encoding . UTF8 . GetString ( _httpClient . UploadData ( uri , "POST" , requestData ) ) ;
160
190
watch . Stop ( ) ;
161
191
162
192
Succeed ( batch , watch . ElapsedMilliseconds ) ;
@@ -187,10 +217,16 @@ public async Task MakeRequest(Batch batch)
187
217
}
188
218
}
189
219
}
220
+
190
221
#else
191
222
watch . Start ( ) ;
192
223
193
- var response = await _httpClient . PostAsync ( uri , new StringContent ( json , Encoding . UTF8 , "application/json" ) ) . ConfigureAwait ( false ) ;
224
+ ByteArrayContent content = new ByteArrayContent ( requestData ) ;
225
+ content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ;
226
+ if ( _client . Config . CompressRequest )
227
+ content . Headers . ContentEncoding . Add ( "gzip" ) ;
228
+
229
+ var response = await _httpClient . PostAsync ( uri , content ) . ConfigureAwait ( false ) ;
194
230
195
231
watch . Stop ( ) ;
196
232
0 commit comments