locked
WebRequest/WebClient: This header must be modified using the appropriate property problem

    Question

  • I need to do a programmatically originated post using WebRequest or WebClient (doesnt really matter)

    When I try to a header into any of the instanciated object, i got the error:
    This header must be modified using the appropriate property problem

    I have read a couple posts by Microsoft's VS2005/2008 developers. They claim to fix the issue caused when using some restricted headers with the WebRequest class which throw the "This header must be modified using the appropriate property" error :

    1- use WebClient instead of webrequest... but webclient still fires that exception if resricted headers are used.

    2- dotnet 3.5 with VS2008 should have a new method for WebRequest called: AddWithoutValidate() which --according to MS-- allows the addition of restricted header type to WebRequest.... However that method does not exists in VS 2008.

     

    Any ideas or solution for overcoming this problem ??

     

    Thanks

    Sunday, May 04, 2008 1:46 AM

All replies

  • Which header specifically are you trying to set? Per the error message, some headers must be set using the corresponding property on the HttpWebRequest itself and not by setting a value in the header collection.

    Monday, May 05, 2008 3:50 PM
  • Well I am trying to set several headers which are listed as restricted:

     

    for example: content-length, user-agent, referer, host and connection...

     

    now using webclient I was able to specify most as properties for example:

    // the code is not accurate just to give you an idea

    WebClient wc = new WebClient();

    wc.UserAgent = "Mozilla etc..etc...etc..";

     

    Now, using webclients works fine mostly except I cant specify the "Host" header neither directly as a property nor within the header collection:
    wc.Headers.Add("Host", "some value");

     

    Thanks

    Monday, May 05, 2008 8:22 PM
  • You're trying to add Content-Length!!  Yikes, so the WebClient/HttpWebRequest sends the data you've provided, formatting it as necessary, perhaps using chunking or compression, and you send some other Content-Length value.   Eeeeeh, the server's going to be very confused.  The same applies to the Connection header.

     

    The Host header has to be the value from the URL, why do you want to override it?

     

    What are you trying to accomplish here?

     

     

    Tuesday, May 06, 2008 11:41 AM
  • The content-length, actually describe the length of the RAW http data, any compression whether IP packed based or gzip is included in different property and it applies a different algorithm.

     

    The Host header is actually --upon using a packet capture software -- not included in the request, that's why I want to add it manually.

    Wednesday, May 07, 2008 10:44 AM
  • Any luck with this?

    I created an overriden WebHeaderCollection which allows me to set restricted values, but when I call this line it doesn't set the Headers class. Any idea what I'm doing wrong? .NET 2.

    request.Headers = headers;

    Wednesday, July 21, 2010 6:21 PM
  • Again, which headers are  you trying to set? As Alan mentioned above, .NEt does not allow you to set headers that it controls. For those you have to use the strong type properties instead, for eg, ContentLength etc.

    For some such as Host header, there is no easy way to change it, unless you upgrade to .NET framework v4.0.


    feroze
    --
    My blog (including System.Net topics

     Subscribe in a reader

    Instruction on how to create a tracelog with your System.Net application
    System.Net Links and HOWTOs
    Wednesday, July 21, 2010 10:21 PM
  • It was the Host header I was trying to set but I've resolved it now after spending a bit of time in Reflector. The WebProxy was one solution but not 100% accurate, so I had to figure out another way.

    .NET 4 would've been easier...

    Thursday, July 22, 2010 8:24 AM
  • care to share what you learned? I too am suffering the Host header problem.
    Friday, September 24, 2010 2:41 PM
  • Hi Steve,

    This issue comes when we try to modify the restricted WebHeaderCollection in WebClient or we type the wrong WebHeader name.

    Following are the allowed list of web header

      // Summary:
            //     The Cache-Control header, which specifies directives that must be obeyed
            //     by all cache control mechanisms along the request/response chain.
            CacheControl = 0,
            //
            // Summary:
            //     The Connection header, which specifies options that are desired for a particular
            //     connection.
            Connection = 1,
            //
            // Summary:
            //     The Date header, which specifies the date and time at which the request originated.
            Date = 2,
            //
            // Summary:
            //     The Keep-Alive header, which specifies a parameter used into order to maintain
            //     a persistent connection.
            KeepAlive = 3,
            //
            // Summary:
            //     The Pragma header, which specifies implementation-specific directives that
            //     might apply to any agent along the request/response chain.
            Pragma = 4,
            //
            // Summary:
            //     The Trailer header, which specifies the header fields present in the trailer
            //     of a message encoded with chunked transfer-coding.
            Trailer = 5,
            //
            // Summary:
            //     The Transfer-Encoding header, which specifies what (if any) type of transformation
            //     that has been applied to the message body.
            TransferEncoding = 6,
            //
            // Summary:
            //     The Upgrade header, which specifies additional communications protocols that
            //     the client supports.
            Upgrade = 7,
            //
            // Summary:
            //     The Via header, which specifies intermediate protocols to be used by gateway
            //     and proxy agents.
            Via = 8,
            //
            // Summary:
            //     The Warning header, which specifies additional information about that status
            //     or transformation of a message that might not be reflected in the message.
            Warning = 9,
            //
            // Summary:
            //     The Allow header, which specifies the set of HTTP methods supported.
            Allow = 10,
            //
            // Summary:
            //     The Content-Length header, which specifies the length, in bytes, of the accompanying
            //     body data.
            ContentLength = 11,
            //
            // Summary:
            //     The Content-Type header, which specifies the MIME type of the accompanying
            //     body data.
            ContentType = 12,
            //
            // Summary:
            //     The Content-Encoding header, which specifies the encodings that have been
            //     applied to the accompanying body data.
            ContentEncoding = 13,
            //
            // Summary:
            //     The Content-Langauge header, which specifies the natural language(s) of the
            //     accompanying body data.
            ContentLanguage = 14,
            //
            // Summary:
            //     The Content-Location header, which specifies a URI from which the accompanying
            //     body may be obtained.
            ContentLocation = 15,
            //
            // Summary:
            //     The Content-MD5 header, which specifies the MD5 digest of the accompanying
            //     body data, for the purpose of providing an end-to-end message integrity check.
            ContentMd5 = 16,
            //
            // Summary:
            //     The Content-Range header, which specifies where in the full body the accompanying
            //     partial body data should be applied.
            ContentRange = 17,
            //
            // Summary:
            //     The Expires header, which specifies the date and time after which the accompanying
            //     body data should be considered stale.
            Expires = 18,
            //
            // Summary:
            //     The Last-Modified header, which specifies the date and time at which the
            //     accompanying body data was last modified.
            LastModified = 19,
            //
            // Summary:
            //     The Accept header, which specifies the MIME types that are acceptable for
            //     the response.
            Accept = 20,
            //
            // Summary:
            //     The Accept-Charset header, which specifies the character sets that are acceptable
            //     for the response.
            AcceptCharset = 21,
            //
            // Summary:
            //     The Accept-Encoding header, which specifies the content encodings that are
            //     acceptable for the response.
            AcceptEncoding = 22,
            //
            // Summary:
            //     The Accept-Langauge header, which specifies that natural languages that are
            //     preferred for the response.
            AcceptLanguage = 23,
            //
            // Summary:
            //     The Authorization header, which specifies the credentials that the client
            //     presents in order to authenticate itself to the server.
            Authorization = 24,
            //
            // Summary:
            //     The Cookie header, which specifies cookie data presented to the server.
            Cookie = 25,
            //
            // Summary:
            //     The Expect header, which specifies particular server behaviors that are required
            //     by the client.
            Expect = 26,
            //
            // Summary:
            //     The From header, which specifies an Internet E-mail address for the human
            //     user who controls the requesting user agent.
            From = 27,
            //
            // Summary:
            //     The Host header, which specifies the host name and port number of the resource
            //     being requested.
            Host = 28,
            //
            // Summary:
            //     The If-Match header, which specifies that the requested operation should
            //     be performed only if the client's cached copy of the indicated resource is
            //     current.
            IfMatch = 29,
            //
            // Summary:
            //     The If-Modified-Since header, which specifies that the requested operation
            //     should be performed only if the requested resource has been modified since
            //     the indicated data and time.
            IfModifiedSince = 30,
            //
            // Summary:
            //     The If-None-Match header, which specifies that the requested operation should
            //     be performed only if none of client's cached copies of the indicated resources
            //     are current.
            IfNoneMatch = 31,
            //
            // Summary:
            //     The If-Range header, which specifies that only the specified range of the
            //     requested resource should be sent, if the client's cached copy is current.
            IfRange = 32,
            //
            // Summary:
            //     The If-Unmodified-Since header, which specifies that the requested operation
            //     should be performed only if the requested resource has not been modified
            //     since the indicated date and time.
            IfUnmodifiedSince = 33,
            //
            // Summary:
            //     The Max-Forwards header, which specifies an integer indicating the remaining
            //     number of times that this request may be forwarded.
            MaxForwards = 34,
            //
            // Summary:
            //     The Proxy-Authorization header, which specifies the credentials that the
            //     client presents in order to authenticate itself to a proxy.
            ProxyAuthorization = 35,
            //
            // Summary:
            //     The Referer header, which specifies the URI of the resource from which the
            //     request URI was obtained.
            Referer = 36,
            //
            // Summary:
            //     The Range header, which specifies the the sub-range(s) of the response that
            //     the client requests be returned in lieu of the entire response.
            Range = 37,
            //
            // Summary:
            //     The TE header, which specifies the transfer encodings that are acceptable
            //     for the response.
            Te = 38,
            //
            // Summary:
            //     The Translate header, a Microsoft extension to the HTTP specification used
            //     in conjunction with WebDAV functionality.
            Translate = 39,
            //
            // Summary:
            //     The User-Agent header, which specifies information about the client agent.
            UserAgent = 40,

    I hope this helps. If you have found any solution then let the community know.

    Thanks

    Babu


    Babu Karan : Technology Specialist: .NET Framework 2.0 Web Applications

    Thursday, June 21, 2012 8:21 AM