Reputation: 1069
I'm trying to interface with a REST interface where there's a named Range header that takes a single value, not a from and to value. I.e., it takes:
Range: dli-depth=1
not
Range: dli-depth=0-1
I can use HttpWebRequest.AddRange() to add the Range header to my HttpWebRequest, but no matter which variant I pick, I always end up with a dash in the value to the right of the equals sign. The device in question won't accept a Range header with a dash in the value. If I attempt to add Range
as a custom header:
myRequest.Headers.Add("Range", $"dli-depth={depth}");
...I get this exception:
System.ArgumentException: 'The 'Range' header must be modified using the appropriate property or method.
How do I add this (perhaps non-standard) Range HTTP header in .NET?
Upvotes: 0
Views: 498
Reputation: 9501
Since you have non-standard header, .net won't allow you to add this header. You can use the following hack:
MethodInfo method = typeof(WebHeaderCollection).GetMethod
("AddWithoutValidate", BindingFlags.Instance | BindingFlags.NonPublic);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(<your url>);
string key = "Range";
string val = $"dli-depth={depth}";
method.Invoke (request.Headers, new object[] { key, val });
Upvotes: 2