0

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?

3
  • 1
    Try this hack stackoverflow.com/a/6580033/1099716 Commented Mar 30, 2020 at 14:19
  • That worked perfectly. Thanks! Care to add that as answer? My question isn't exactly the same as the one you referenced, but the answer is the same. Commented Mar 30, 2020 at 23:26
  • Great, I've added answer. Commented Mar 31, 2020 at 3:20

1 Answer 1

2

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 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.