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"