Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need the short and technical answer go right to the last section of the answer.</p> <p>If you want to know better, read it all, and i hope you'll enjoy...</p> <hr> <p>I countered this problem too today, and what i discovered today is that:</p> <ol> <li><p>the above answers are true, as:</p> <p>1.1 it's telling you that the header you are trying to add already exist and you should then modify its value using the appropriate property (the indexer, for instance), instead of trying to add it again.</p> <p>1.2 Anytime you're changing the headers of an <code>HttpWebRequest</code>, you need to use the appropriate properties on the object itself, if they exist.</p></li> </ol> <p>Thanks FOR and Jvenema for the leading guidelines...</p> <ol start="2"> <li><p>But, What i found out, and <strong>that was the missing piece in the puzzle</strong> is that:</p> <p>2.1 The <code>WebHeaderCollection</code> class is generally accessed through <code>WebRequest</code>.Headers or <code>WebResponse</code>.Headers. <strong>Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed.</strong></p></li> </ol> <p>The restricted headers are:</p> <ul> <li><code>Accept</code></li> <li><code>Connection</code></li> <li><code>Content-Length</code></li> <li><code>Content-Type</code></li> <li><code>Date</code></li> <li><code>Expect</code></li> <li><code>Host</code></li> <li><code>If-Modified-Since</code></li> <li><code>Range</code></li> <li><code>Referer</code></li> <li><code>Transfer-Encoding</code></li> <li><code>User-Agent</code></li> <li><code>Proxy-Connection</code></li> </ul> <hr> <p>So, next time you are facing this exception and don't know how to solve this, remember that there are some restricted headers, and the solution is to modify their values using the appropriate property explicitly from the <code>WebRequest</code>/<code>HttpWebRequest</code> class.</p> <hr> <p>Edit: (useful, from comments, comment by user <a href="https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest#comment15470171_4752359">Kaido</a>)</p> <blockquote> <p>Solution is to check if the header exists already or is restricted (<code>WebHeaderCollection.IsRestricted(key)</code>) before calling add</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload