13
13
using System . Reflection ;
14
14
using System . Reflection . Emit ;
15
15
using System . Runtime . InteropServices ;
16
+ using System . Net ;
16
17
17
18
#if NETSTANDARD1_3
18
19
using System . Collections . Specialized ;
19
- using System . Net ;
20
20
using System . Linq . Expressions ;
21
21
#endif
22
22
@@ -54,6 +54,27 @@ public class NetStandardPclExport : PclExport
54
54
"--MM--zzzzzz" ,
55
55
} ;
56
56
57
+ static readonly Action < HttpWebRequest , string > SetUserAgentDelegate =
58
+ ( Action < HttpWebRequest , string > ) typeof ( HttpWebRequest )
59
+ . GetProperty ( "UserAgent" )
60
+ ? . SetMethod ( ) ? . CreateDelegate ( typeof ( Action < HttpWebRequest , string > ) ) ;
61
+
62
+ static readonly Action < HttpWebRequest , bool > SetAllowAutoRedirectDelegate =
63
+ ( Action < HttpWebRequest , bool > ) typeof ( HttpWebRequest )
64
+ . GetProperty ( "AllowAutoRedirect" )
65
+ ? . SetMethod ( ) ? . CreateDelegate ( typeof ( Action < HttpWebRequest , bool > ) ) ;
66
+
67
+ static readonly Action < HttpWebRequest , bool > SetKeepAliveDelegate =
68
+ ( Action < HttpWebRequest , bool > ) typeof ( HttpWebRequest )
69
+ . GetProperty ( "KeepAlive" )
70
+ ? . SetMethod ( ) ? . CreateDelegate ( typeof ( Action < HttpWebRequest , bool > ) ) ;
71
+
72
+ static readonly Action < HttpWebRequest , long > SetContentLengthDelegate =
73
+ ( Action < HttpWebRequest , long > ) typeof ( HttpWebRequest )
74
+ . GetProperty ( "ContentLength" )
75
+ ? . SetMethod ( ) ? . CreateDelegate ( typeof ( Action < HttpWebRequest , long > ) ) ;
76
+
77
+
57
78
public NetStandardPclExport ( )
58
79
{
59
80
this . PlatformName = Platforms . NetStandard ;
@@ -470,17 +491,48 @@ public override ParseStringDelegate GetJsReaderParseMethod<TSerializer>(Type typ
470
491
return null ;
471
492
}
472
493
473
- #if NETSTANDARD1_3
494
+ public override void SetUserAgent ( HttpWebRequest httpReq , string value )
495
+ {
496
+ if ( SetUserAgentDelegate != null )
497
+ {
498
+ SetUserAgentDelegate ( httpReq , value ) ;
499
+ } else
500
+ {
501
+ httpReq . Headers [ HttpRequestHeader . UserAgent ] = value ;
502
+ }
503
+ }
504
+
505
+ public override void SetContentLength ( HttpWebRequest httpReq , long value )
506
+ {
507
+ if ( SetContentLengthDelegate != null )
508
+ {
509
+ SetContentLengthDelegate ( httpReq , value ) ;
510
+ } else
511
+ {
512
+ httpReq . Headers [ HttpRequestHeader . ContentLength ] = value . ToString ( ) ;
513
+ }
514
+ }
515
+
516
+ public override void SetAllowAutoRedirect ( HttpWebRequest httpReq , bool value )
517
+ {
518
+ SetAllowAutoRedirectDelegate ? . Invoke ( httpReq , value ) ;
519
+ }
520
+
521
+ public override void SetKeepAlive ( HttpWebRequest httpReq , bool value )
522
+ {
523
+ SetKeepAliveDelegate ? . Invoke ( httpReq , value ) ;
524
+ }
525
+
474
526
public override void InitHttpWebRequest ( HttpWebRequest httpReq ,
475
527
long ? contentLength = null , bool allowAutoRedirect = true , bool keepAlive = true )
476
528
{
477
- httpReq . Headers [ HttpRequestHeader . UserAgent ] = Env . ServerUserAgent ;
478
- // httpReq.AllowAutoRedirect = allowAutoRedirect;
479
- // httpReq.KeepAlive = keepAlive;
529
+ SetUserAgent ( httpReq , Env . ServerUserAgent ) ;
530
+ SetAllowAutoRedirect ( httpReq , allowAutoRedirect ) ;
531
+ SetKeepAlive ( httpReq , keepAlive ) ;
480
532
481
533
if ( contentLength != null )
482
534
{
483
- httpReq . Headers [ HttpRequestHeader . ContentLength ] = contentLength . Value . ToString ( ) ;
535
+ SetContentLength ( httpReq , contentLength . Value ) ;
484
536
}
485
537
}
486
538
@@ -492,13 +544,14 @@ public override void Config(HttpWebRequest req,
492
544
bool ? preAuthenticate = null )
493
545
{
494
546
//req.MaximumResponseHeadersLength = int.MaxValue; //throws "The message length limit was exceeded" exception
495
- // if (allowAutoRedirect.HasValue) req.AllowAutoRedirect = allowAutoRedirect.Value;
547
+ if ( allowAutoRedirect . HasValue ) SetAllowAutoRedirect ( req , allowAutoRedirect . Value ) ;
496
548
//if (readWriteTimeout.HasValue) req.ReadWriteTimeout = (int)readWriteTimeout.Value.TotalMilliseconds;
497
549
//if (timeout.HasValue) req.Timeout = (int)timeout.Value.TotalMilliseconds;
498
550
if ( userAgent != null ) req . Headers [ HttpRequestHeader . UserAgent ] = userAgent ;
499
551
//if (preAuthenticate.HasValue) req.PreAuthenticate = preAuthenticate.Value;
500
552
}
501
553
554
+ #if NETSTANDARD1_3
502
555
public override string GetStackTrace ( )
503
556
{
504
557
return Environment . StackTrace ;
0 commit comments