Reflection: Modify the date value of the request header Httpwebrequest/webclient Header Property-"This header must be modified with the appropriate properties"

Source: Internet
Author: User
Tags stack trace

Scenario: Calling the external interface, the interface requires the header information to contain the date information, and the date information must be formatted well, (they use Java), but the default is that the date property cannot be modified,

Therefore, the following error will occur:

Unhandled exception: System.Net.WebException: An exception occurred during the WebClient request. ---> Sy
Tem. ArgumentException: This header must be modified with the appropriate properties.
Parameter name: Name
In System.Net.WebHeaderCollection.ThrowOnRestrictedHeader (String headername)
In System.Net.WebHeaderCollection.Add (string name, String value)
In System.Net.HttpWebRequest.set_Headers (webheadercollection value)
At System.Net.WebClient.CopyHeadersTo (WebRequest request)
In System.Net.WebClient.GetWebRequest (Uri address)
In System.Net.WebClient.DownloadFile (Uri address, String FileName)
---the end of the inner exception stack trace---
In System.Net.WebClient.DownloadFile (Uri address, String FileName)
In System.Net.WebClient.DownloadFile (string address, string fileName)
In Hash. Program.main (string[] args) location D:\powerall\hash\hash\Program.cs: line
No. 122

Workaround Code:

MethodInfo Primethod = WebReqst.Headers.GetType (). GetMethod ("addwithoutvalidate", BindingFlags.Instance |  BindingFlags.NonPublic);                 New " Date " , DATE});                 string mydate = WebReqst.Headers.Get ("Date");

As you can see, the solution to the problem is to use reflection.

Reflection :

The ability of a program to access, detect, and modify its own state or behavior. Assemblies contain modules, and modules contain types, and types also contain members. Reflection provides an object that encapsulates assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get a type from an existing object. You can then invoke a method of the type or access its fields and properties. Advantages: 1, reflection improves the flexibility and extensibility of the program. 2, reduce the coupling, improve the adaptive ability. 3. It allows the program to create and control objects of any class without having to hardcode the target class in advance.
Disadvantages:
1. Performance issues: Using reflection is basically an interpretation operation that is used for fields and methods that are much slower to access than direct code. Therefore, the reflection mechanism is mainly used in the system framework of high flexibility and extensibility, which is not recommended in general procedure. 2, the use of reflection will blur the internal logic of the program; programmers want to see the logic of the program in the source code, but the reflection bypasses the technology of the source code, thus bringing about maintenance problems, and the reflection code is more complex than the corresponding direct code.

Reflection: Modify the date value of the request header Httpwebrequest/webclient Header Property-"This header must be modified with the appropriate properties"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.