This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24690|Reply: 1

[Source] The "host" header must be modified with the appropriate attributes or methods

[Copy link]
Posted on 3/21/2017 7:50:12 PM | | |
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:
HeaderSet up
AcceptAccept property.
ConnectionSet by the Connection property and the KeepAlive property.
Content-LengthSet by the ContentLength property.
Content-TypeSetby the ContentType property.
ExpectSet by the Expect property.
DateSet by the Date property.
HostHost property.
If-Modified-SinceSet by the IfModifiedSince property.
rangeSet by the AddRange method.
RefererSet by the Referer property.
Transfer-EncodingSet by the TransferEncoding property (the SendChunked property must be true).
User-AgentUserAgent property.





Previous:XDU Meter Real-time Internet Speed Monitoring [Source Code]
Next:MVC This Virtual Directory does not allow contents to be listed.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com