Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 32a7317

Browse files
committed
Add ability to set up HttpWebRequest properties
Most of HttpWebRequest properties are not implemented on .NET Core, but they are exist on other platforms such as Xamarin and .NET, so we can try to find the properties by Reflection and call setter on them, if property does not exists just do usual way
1 parent 29b2bd8 commit 32a7317

File tree

4 files changed

+103
-20
lines changed

4 files changed

+103
-20
lines changed

src/ServiceStack.Text/PclExport.Net40.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,26 @@ public override void Config(HttpWebRequest req,
545545
if (preAuthenticate.HasValue) req.PreAuthenticate = preAuthenticate.Value;
546546
}
547547

548+
public override void SetUserAgent(HttpWebRequest httpReq, string value)
549+
{
550+
httpReq.UserAgent = value;
551+
}
552+
553+
public override void SetContentLength(HttpWebRequest httpReq, long value)
554+
{
555+
httpReq.ContentLength = value;
556+
}
557+
558+
public override void SetAllowAutoRedirect(HttpWebRequest httpReq, bool value)
559+
{
560+
httpReq.AllowAutoRedirect = value;
561+
}
562+
563+
public override void SetKeepAlive(HttpWebRequest httpReq, bool value)
564+
{
565+
httpReq.KeepAlive = value;
566+
}
567+
548568
public override string GetStackTrace()
549569
{
550570
return Environment.StackTrace;

src/ServiceStack.Text/PclExport.NetStandard.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
using System.Reflection;
1414
using System.Reflection.Emit;
1515
using System.Runtime.InteropServices;
16+
using System.Net;
1617

1718
#if NETSTANDARD1_3
1819
using System.Collections.Specialized;
19-
using System.Net;
2020
using 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;

src/ServiceStack.Text/PclExport.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,24 @@ public virtual void AddHeader(WebRequest webReq, string name, string value)
264264
webReq.Headers[name] = value;
265265
}
266266

267+
public virtual void SetUserAgent(HttpWebRequest httpReq, string value)
268+
{
269+
httpReq.Headers[HttpRequestHeader.UserAgent] = value;
270+
}
271+
272+
public virtual void SetContentLength(HttpWebRequest httpReq, long value)
273+
{
274+
httpReq.Headers[HttpRequestHeader.ContentLength] = value.ToString();
275+
}
276+
277+
public virtual void SetAllowAutoRedirect(HttpWebRequest httpReq, bool value)
278+
{
279+
}
280+
281+
public virtual void SetKeepAlive(HttpWebRequest httpReq, bool value)
282+
{
283+
}
284+
267285
public virtual Assembly[] GetAllAssemblies()
268286
{
269287
return new Assembly[0];

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,11 @@ internal static TypeInfo GetTypeInfo(this Type type)
8888
#endif
8989

9090
#if NETSTANDARD1_1
91-
private static readonly Func<Type, object> GetUninitializedObjectDelegate;
92-
93-
static ReflectionExtensions()
94-
{
95-
var formatterServices = typeof(string).GetTypeInfo().Assembly
96-
.GetType("System.Runtime.Serialization.FormatterServices");
97-
if (formatterServices != null)
98-
{
99-
var method = formatterServices.GetMethod("GetUninitializedObject");
100-
if (method != null)
101-
GetUninitializedObjectDelegate = (Func<Type, object>)method.CreateDelegate(typeof(Func<Type, object>));
102-
}
103-
}
91+
private static readonly Func<Type, object> GetUninitializedObjectDelegate =
92+
(Func<Type, object>) typeof(string).GetTypeInfo().Assembly
93+
.GetType("System.Runtime.Serialization.FormatterServices")
94+
?.GetMethod("GetUninitializedObject")
95+
?.CreateDelegate(typeof(Func<Type, object>));
10496
#endif
10597

10698
public static TypeCode GetTypeCode(this Type type)

0 commit comments

Comments
 (0)