@@ -34,8 +34,7 @@ namespace RestSharp
34
34
/// </summary>
35
35
public partial class Http : IHttp , IHttpFactory
36
36
{
37
- private const string _lineBreak = "\r \n " ;
38
- private static readonly Encoding _defaultEncoding = Encoding . UTF8 ;
37
+ private const string LINE_BREAK = "\r \n " ;
39
38
40
39
///<summary>
41
40
/// Creates an IHttp
@@ -148,6 +147,9 @@ protected bool HasFiles
148
147
/// </summary>
149
148
public bool UseDefaultCredentials { get ; set ; }
150
149
#endif
150
+ private Encoding encoding = Encoding . UTF8 ;
151
+
152
+ public Encoding Encoding { get { return this . encoding ; } set { this . encoding = value ; } }
151
153
152
154
/// <summary>
153
155
/// HTTP headers to be sent with request
@@ -201,15 +203,15 @@ protected bool HasFiles
201
203
/// </summary>
202
204
public Http ( )
203
205
{
204
- Headers = new List < HttpHeader > ( ) ;
205
- Files = new List < HttpFile > ( ) ;
206
- Parameters = new List < HttpParameter > ( ) ;
207
- Cookies = new List < HttpCookie > ( ) ;
206
+ this . Headers = new List < HttpHeader > ( ) ;
207
+ this . Files = new List < HttpFile > ( ) ;
208
+ this . Parameters = new List < HttpParameter > ( ) ;
209
+ this . Cookies = new List < HttpCookie > ( ) ;
208
210
209
- _restrictedHeaderActions = new Dictionary < string , Action < HttpWebRequest , string > > ( StringComparer . OrdinalIgnoreCase ) ;
211
+ restrictedHeaderActions = new Dictionary < string , Action < HttpWebRequest , string > > ( StringComparer . OrdinalIgnoreCase ) ;
210
212
211
- AddSharedHeaderActions ( ) ;
212
- AddSyncHeaderActions ( ) ;
213
+ this . AddSharedHeaderActions ( ) ;
214
+ this . AddSyncHeaderActions ( ) ;
213
215
}
214
216
215
217
partial void AddSyncHeaderActions ( ) ;
@@ -218,10 +220,10 @@ public Http()
218
220
219
221
private void AddSharedHeaderActions ( )
220
222
{
221
- _restrictedHeaderActions . Add ( "Accept" , ( r , v ) => r . Accept = v ) ;
222
- _restrictedHeaderActions . Add ( "Content-Type" , ( r , v ) => r . ContentType = v ) ;
223
+ restrictedHeaderActions . Add ( "Accept" , ( r , v ) => r . Accept = v ) ;
224
+ restrictedHeaderActions . Add ( "Content-Type" , ( r , v ) => r . ContentType = v ) ;
223
225
#if NET4
224
- _restrictedHeaderActions . Add ( "Date" , ( r , v ) =>
226
+ restrictedHeaderActions . Add ( "Date" , ( r , v ) =>
225
227
{
226
228
DateTime parsed ;
227
229
@@ -231,28 +233,28 @@ private void AddSharedHeaderActions()
231
233
}
232
234
} ) ;
233
235
234
- _restrictedHeaderActions . Add ( "Host" , ( r , v ) => r . Host = v ) ;
236
+ restrictedHeaderActions . Add ( "Host" , ( r , v ) => r . Host = v ) ;
235
237
#else
236
- _restrictedHeaderActions . Add ( "Date" , ( r , v ) => { /* Set by system */ } ) ;
237
- _restrictedHeaderActions . Add ( "Host" , ( r , v ) => { /* Set by system */ } ) ;
238
+ restrictedHeaderActions . Add ( "Date" , ( r , v ) => { /* Set by system */ } ) ;
239
+ restrictedHeaderActions . Add ( "Host" , ( r , v ) => { /* Set by system */ } ) ;
238
240
#endif
239
241
240
242
#if FRAMEWORK
241
- _restrictedHeaderActions . Add ( "Range" , ( r , v ) => { AddRange ( r , v ) ; } ) ;
243
+ restrictedHeaderActions . Add ( "Range" , ( r , v ) => { AddRange ( r , v ) ; } ) ;
242
244
#endif
243
245
}
244
246
245
- private const string FormBoundary = "-----------------------------28947758029299" ;
247
+ private const string FORM_BOUNDARY = "-----------------------------28947758029299" ;
246
248
247
249
private static string GetMultipartFormContentType ( )
248
250
{
249
- return string . Format ( "multipart/form-data; boundary={0}" , FormBoundary ) ;
251
+ return string . Format ( "multipart/form-data; boundary={0}" , FORM_BOUNDARY ) ;
250
252
}
251
253
252
254
private static string GetMultipartFileHeader ( HttpFile file )
253
255
{
254
256
return string . Format ( "--{0}{4}Content-Disposition: form-data; name=\" {1}\" ; filename=\" {2}\" {4}Content-Type: {3}{4}{4}" ,
255
- FormBoundary , file . Name , file . FileName , file . ContentType ?? "application/octet-stream" , _lineBreak ) ;
257
+ FORM_BOUNDARY , file . Name , file . FileName , file . ContentType ?? "application/octet-stream" , LINE_BREAK ) ;
256
258
}
257
259
258
260
private string GetMultipartFormData ( HttpParameter param )
@@ -261,25 +263,25 @@ private string GetMultipartFormData(HttpParameter param)
261
263
? "--{0}{3}Content-Type: {1}{3}Content-Disposition: form-data; name=\" {1}\" {3}{3}{2}{3}"
262
264
: "--{0}{3}Content-Disposition: form-data; name=\" {1}\" {3}{3}{2}{3}" ;
263
265
264
- return string . Format ( format , FormBoundary , param . Name , param . Value , _lineBreak ) ;
266
+ return string . Format ( format , FORM_BOUNDARY , param . Name , param . Value , LINE_BREAK ) ;
265
267
}
266
268
267
269
private static string GetMultipartFooter ( )
268
270
{
269
- return string . Format ( "--{0}--{1}" , FormBoundary , _lineBreak ) ;
271
+ return string . Format ( "--{0}--{1}" , FORM_BOUNDARY , LINE_BREAK ) ;
270
272
}
271
273
272
- private readonly IDictionary < string , Action < HttpWebRequest , string > > _restrictedHeaderActions ;
274
+ private readonly IDictionary < string , Action < HttpWebRequest , string > > restrictedHeaderActions ;
273
275
274
276
// handle restricted headers the .NET way - thanks @dimebrain!
275
277
// http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx
276
278
private void AppendHeaders ( HttpWebRequest webRequest )
277
279
{
278
280
foreach ( var header in Headers )
279
281
{
280
- if ( _restrictedHeaderActions . ContainsKey ( header . Name ) )
282
+ if ( restrictedHeaderActions . ContainsKey ( header . Name ) )
281
283
{
282
- _restrictedHeaderActions [ header . Name ] . Invoke ( webRequest , header . Value ) ;
284
+ restrictedHeaderActions [ header . Name ] . Invoke ( webRequest , header . Value ) ;
283
285
}
284
286
else
285
287
{
@@ -356,9 +358,9 @@ private void PreparePostBody(HttpWebRequest webRequest)
356
358
}
357
359
}
358
360
359
- private static void WriteStringTo ( Stream stream , string toWrite )
361
+ private void WriteStringTo ( Stream stream , string toWrite )
360
362
{
361
- var bytes = _defaultEncoding . GetBytes ( toWrite ) ;
363
+ var bytes = this . Encoding . GetBytes ( toWrite ) ;
362
364
stream . Write ( bytes , 0 , bytes . Length ) ;
363
365
}
364
366
@@ -376,7 +378,7 @@ private void WriteMultipartFormData(Stream requestStream)
376
378
377
379
// Write the file data directly to the Stream, rather than serializing it to a string.
378
380
file . Writer ( requestStream ) ;
379
- WriteStringTo ( requestStream , _lineBreak ) ;
381
+ WriteStringTo ( requestStream , LINE_BREAK ) ;
380
382
}
381
383
382
384
WriteStringTo ( requestStream , GetMultipartFooter ( ) ) ;
0 commit comments