When using the httpwebrequest and webrequest class in C#, if you try to set the header of the http request, whether you use the set method or the add method, such as setting the host property in the header:
will error: "This header must be modified with the appropriate attribute or method".
// Summary: Get or set the Host header value to be used independently of the request URI in an HTTP request. // Return results: Host header value in HTTP requests. // Anomaly: // System.ArgumentNullException: Host headers cannot be set to null. // // System.ArgumentException: Host headers cannot be set to invalid values. // // System.InvalidOperationException: Host headers cannot be set after you have started sending System.Net.HttpWebRequests. public string Host { get; set; }
Reason: C# doesn't allow you to use the set and add methods to set such headers C# already provides a dedicated attribute for such a header that you can use when modifying and setting this header. The header name and corresponding attributes are set as follows: | Header | Set up | | Accept | Accept property. | | Connection | Set by the Connection property and the KeepAlive property. | | Content-Length | Set by the ContentLength property. | | Content-Type | Setby the ContentType property. | | Expect | Set by the Expect property. | | Date | Set by the Date property. | | Host | Host property. | | If-Modified-Since | Set by the IfModifiedSince property. | | range | Set by the AddRange method. | | Referer | Set by the Referer property. | | Transfer-Encoding | Set by the TransferEncoding property (the SendChunked property must be true). | | User-Agent | UserAgent property. |
|