@@ -74,6 +74,7 @@ public class NetStandardPclExport : PclExport
74
74
. GetProperty ( "ContentLength" )
75
75
? . SetMethod ( ) ? . CreateDelegate ( typeof ( Action < HttpWebRequest , long > ) ) ;
76
76
77
+ private bool allowToChangeRestrictedHeaders ;
77
78
78
79
public NetStandardPclExport ( )
79
80
{
@@ -83,6 +84,15 @@ public NetStandardPclExport()
83
84
#else
84
85
this . DirSep = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? '\\ ' : '/' ;
85
86
#endif
87
+ var req = HttpWebRequest . Create ( "http://servicestack.net" ) ;
88
+ try
89
+ {
90
+ req . Headers [ HttpRequestHeader . UserAgent ] = "ServiceStack" ;
91
+ allowToChangeRestrictedHeaders = true ;
92
+ } catch ( ArgumentException )
93
+ {
94
+ allowToChangeRestrictedHeaders = false ;
95
+ }
86
96
}
87
97
88
98
public override string ReadAllText ( string filePath )
@@ -498,7 +508,8 @@ public override void SetUserAgent(HttpWebRequest httpReq, string value)
498
508
SetUserAgentDelegate ( httpReq , value ) ;
499
509
} else
500
510
{
501
- httpReq . Headers [ HttpRequestHeader . UserAgent ] = value ;
511
+ if ( allowToChangeRestrictedHeaders )
512
+ httpReq . Headers [ HttpRequestHeader . UserAgent ] = value ;
502
513
}
503
514
}
504
515
@@ -509,7 +520,8 @@ public override void SetContentLength(HttpWebRequest httpReq, long value)
509
520
SetContentLengthDelegate ( httpReq , value ) ;
510
521
} else
511
522
{
512
- httpReq . Headers [ HttpRequestHeader . ContentLength ] = value . ToString ( ) ;
523
+ if ( allowToChangeRestrictedHeaders )
524
+ httpReq . Headers [ HttpRequestHeader . ContentLength ] = value . ToString ( ) ;
513
525
}
514
526
}
515
527
@@ -547,7 +559,7 @@ public override void Config(HttpWebRequest req,
547
559
if ( allowAutoRedirect . HasValue ) SetAllowAutoRedirect ( req , allowAutoRedirect . Value ) ;
548
560
//if (readWriteTimeout.HasValue) req.ReadWriteTimeout = (int)readWriteTimeout.Value.TotalMilliseconds;
549
561
//if (timeout.HasValue) req.Timeout = (int)timeout.Value.TotalMilliseconds;
550
- if ( userAgent != null ) req . Headers [ HttpRequestHeader . UserAgent ] = userAgent ;
562
+ if ( userAgent != null ) SetUserAgent ( req , userAgent ) ;
551
563
//if (preAuthenticate.HasValue) req.PreAuthenticate = preAuthenticate.Value;
552
564
}
553
565
0 commit comments