When adding headers to a HttpWebRequest or HttpWebResponse, you may have come across the error “This header must be modified using the appropriate property.” Some headers are restricted from simply setting via the headers collection.
[more Click to read more…]
Server Error in '/' Application.
This header must be modified using the appropriate property.
Parameter name: name
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This header must be modified using the appropriate property.
Parameter name: name
Source Error:
Line 35: { Line 36: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(target); Line 37: request.Headers.Add(context.Request.Headers); Line 38: using (StreamReader reader = new StreamReader(context.Request.InputStream)) Line 39: {
|
Source File: C:\temp\RequestProxy.cs Line: 37
Stack Trace:
[ArgumentException: This header must be modified using the appropriate property. Parameter name: name] System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName) +5370110 System.Net.WebHeaderCollection.Add(String name, String value) +32 System.Collections.Specialized.NameValueCollection.Add(NameValueCollection c) +108 Sfx.Web.Handlers.RequestProxy.ProcessRequest(HttpContext context) in C:\temp\RequestProxy.cs:37 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
|
Version Information: Microsoft .NET Framework Version:2; ASP.NET Version:2
You can see above that copying the headers collection from one request into another is going to cause the problem. The following is a list of request and response headers that must be set via properties instead of headers collection. These properties will be translated to headers on the wire:
|
Header
|
Restricted on Request
|
Restricted on Response
|
| Accept |
true – use .Accept property |
false |
| Connection |
true – use .Connection property |
false |
| Content-Type |
true – use .ContentType property |
false |
| Content-Length |
true – use .ContentLength property |
true – use .ContentLength property |
| Date |
true |
false |
| Expect |
true – use .Expect property |
false |
| Host |
true |
false |
| If-Modified-Since |
true – use .IfModifiedSince property |
false |
| Keep-Alive |
false |
true |
| Proxy-Connection |
true – use .Proxy property |
false |
| Range |
true |
false |
| Referer |
true – use .Referer property |
false |
| Transfer-Encoding |
true – use .TransferEncoding property |
true |
| User-Agent |
true – use .UserAgent property |
false |
| WWW-Authenticate |
false |
true |
507619c8-3a53-4848-be0a-2dfb78279c76|2|3.0
Development
.net