1313using System . Reflection ;
1414using System . Reflection . Emit ;
1515using System . Runtime . InteropServices ;
16+ using System . Net ;
1617
1718#if NETSTANDARD1_3
1819using System . Collections . Specialized ;
19- using System . Net ;
2020using System . Linq . Expressions ;
2121#endif
2222
@@ -54,6 +54,27 @@ public class NetStandardPclExport : PclExport
5454 "--MM--zzzzzz" ,
5555 } ;
5656
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+
5778 public NetStandardPclExport ( )
5879 {
5980 this . PlatformName = Platforms . NetStandard ;
@@ -470,17 +491,48 @@ public override ParseStringDelegate GetJsReaderParseMethod<TSerializer>(Type typ
470491 return null ;
471492 }
472493
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+
474526 public override void InitHttpWebRequest ( HttpWebRequest httpReq ,
475527 long ? contentLength = null , bool allowAutoRedirect = true , bool keepAlive = true )
476528 {
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 ) ;
480532
481533 if ( contentLength != null )
482534 {
483- httpReq . Headers [ HttpRequestHeader . ContentLength ] = contentLength . Value . ToString ( ) ;
535+ SetContentLength ( httpReq , contentLength . Value ) ;
484536 }
485537 }
486538
@@ -492,13 +544,14 @@ public override void Config(HttpWebRequest req,
492544 bool ? preAuthenticate = null )
493545 {
494546 //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 ) ;
496548 //if (readWriteTimeout.HasValue) req.ReadWriteTimeout = (int)readWriteTimeout.Value.TotalMilliseconds;
497549 //if (timeout.HasValue) req.Timeout = (int)timeout.Value.TotalMilliseconds;
498550 if ( userAgent != null ) req . Headers [ HttpRequestHeader . UserAgent ] = userAgent ;
499551 //if (preAuthenticate.HasValue) req.PreAuthenticate = preAuthenticate.Value;
500552 }
501553
554+ #if NETSTANDARD1_3
502555 public override string GetStackTrace ( )
503556 {
504557 return Environment . StackTrace ;
0 commit comments