Prerequisite Description:
1, Str.empty is actually a string. Empty
2, @object. The IsNull () of IsNull () is an extension method that determines whether the "equals null" is equal to the @object ==null?true:false;
3, result. IsEmpty () of IsEmpty () is an extension method that determines whether a string is equal to an empty string or null
First, I want to be the HTTP request can be directly thrown into the object, the program automatically help me to handle the parameters well, it is too cool.
So the internet to find data found using reflection can help me realize this idea, here is my search and adjust the method.
Reflection Reflection Operation
Description
1, tostr<t> (list<t> @object, char differentiate = ' = ', char separator = ' & ') where T:new () is the processing collection class Type Object into a string method, for example: a[0].a=xxx&b[0].b=xxx
2, tostr<t> (T @object, char differentiate = ' = ', char separator = ' & ') where T:new () is processing a single object conversion to a string method, for example: a.a=xxx&a.b=xxx
3, toliststrbytemplate<t> (list<t> @object, string template) where T:new () is processed according to the template processing The collection type Object is converted to a string collection method, for example: [A[0].a=xxx,b[0].b=xxx]
4, toliststrbytemplate<t> (T @object, string template) where T:new () is processed according to the template processing a single Object into a string collection method, for example: [A.a=xxx,a.b=xxx]
Unit tests (because reflection is a string is called reflection as a string collection method, so only partial unit tests that reflect a string method are posted):
reflection as a string unit test
By this time, the biggest problem has been solved. Look, the complex object program has been able to help me assemble it automatically.
In fact, so far almost can be finished, but who is a small meow is lazy, but also want to use the HTTP tool method easier to use, when the simple a few clicks on the OK, and therefore embarked on the package (should be called) HTTP tool method of "no return" on the ^_^.
Second, want the HTTP request method to be more useful
1, the first is the base class
HTTP request Base Interface
HTTP request Operation abstract base class "partial class"
HTTP request Operation abstract base class "partial class"
The abstract base class is a common part of extracting HTTP requests get and post methods, and the Get method and post action method classes inherit the base class and implement their own specific functionality by overriding the method.
I divide the HTTP request into three kinds: Get method, Post method (the kind that is popular on the net, can pass string or stitching "a=xx&b=xx" such as String parameter), Post request form form and file
So I'm divided into three classes to implement these three request methods
2, three kinds of realization class
A, GET request operation
httpget Request Operation Interface
HttpGet Request Operation "Implementation class"
b, POST request operation
httppost Request Operation Interface
HttpPost Request Operation "Partial implementation class"
HttpPost Request Operation "Partial implementation class"
C, Post form request operation
httppost Form Request Operation Interface
httppost Form Request Operation "Partial implementation class"
httppost Form Request Operation "Partial implementation class"
HttpPost Form file Item class
OK, the preparation is complete!!! Of course now, it will be very troublesome to use, so we need a unified access to the class to wrap them up, so that the whole tool can be more convenient to use.
3. Unified Access Class
1// <summary> 2// Request network Resource Operations 3// </summary> 4 Public class HttpRequest 5 {6 // <summary> 7// GET request 8// </summary> 9// <param name= "url" > Request address </param>10 Public static Ihttpget Get (string url) one by one { new HttpGet (URL),}14// <summary>16// POST request/// </summary >18 //<param name= "url" > Request address </param>19 public static ihttppost Post (string url) return new HttpPost (URL),}23//<summary>25/// Post form request/// </summary >27 //<param name= "url" > Request address </param>28 public static ihttppostform postform (string url) 29 { return new httppostform (URL); }32
OK, so far the entire HTTP request tool is packaged and finished!
My code structure is like this!
4. Usage
1//<summary> 2//test Postform Request 3//</summary> 4 [TestMethod] 5 public void Testpostform () 6 {7 list<test_person> List = new List<test_person> 8 {9 new Test_person10 {One Name = "31", age = 32,13 Sex = test_sextype.man,14 Friend = new[] {"A", "B", "C"},15 Guids = new[] {n EW GUID ("118d5bb1-1b61-467a-8d7b-42fe42df9e5a"), New GUID ("73a02a5b-eaff-45d2-a243-7fa6c5f4792f"), New GUID (" 53e4bd2f-253e-4db7-96a5-e93e62da5a29 ")},16 Birthday = new DateTime (2016,7,14,10,54,32), 17 Achilds = new[]18 {new TEST_CHILD20 {21 Name = "Zhang Xiao 311", age = 20,23 Sex = test_sextype.woman,24 25 },26 New test_child27 {312 = "Zhang Xiao", age = 20,30 Sex = test_sextype.woman,31}33},34 Childs = new list<test_child>35 {new Test_child37 {38 Name = "Zhang Xiao 321", age = 20,40 Sex = test_sextype.woman,41 42 },43 new Test_child44 {"322", 4 6 age = 20,47 Sex = test_sextype.woman,48 49}50 }51},52 new Test_person53 {si Name = "32", 55 Age = 32,56 Sex = test_sextype.man,57 Friend = new[] {"A", "B", "C"},58 Guids = new[] {nEW GUID ("118d5bb1-1b61-467a-8d7b-42fe42df9e5a"), New GUID ("73a02a5b-eaff-45d2-a243-7fa6c5f4792f"), New GUID (" 53e4bd2f-253e-4db7-96a5-e93e62da5a29 ")},59 Birthday = new DateTime (2016,7,14,10,54,32), 60 Achilds = new[]61 {test_child63 {64 Name = "Zhang Xiao 311", 20,66 Sex = test_sextype.woman,67 68 },69 new TEST_CHILD70 {"312", 72 Age = 20,73 Sex = test_sextype.woman,74 75}76 },77 Childs = new list<test_child>78 {new TEST_CHILD80 {Bayi Name = "Zhang Xiao 321", 20,83 age = Sex = test_sextype.woman,84 85 },86 new Test_child87 {The Name = "Zhang Xiao 322", 8 9 age = 20,90 Sex = test_sextype.woman,91 92}93 }94}95};96 var result = Meow.Webs.HttpRequest.PostForm ("http://localhost: 63694/app/system/test "). Addlinkdata (list). AddData (list). AddData ("name", "Zhang San"). Addfiledata ("Testfile", "F:\\mediaresources\\video\\123456.avi"). Run (); 97}
Too many unit test codes are not listed.
In short, it is like the use of Postform forms. The GET request parameter is added to the URL, so the Add method name is Addlinkdata (), post can also be in the URL with parameters, so the Post method also inherits the Addlinkdata () method.
Add parameter method currently only support these kinds, if there are other requirements can contact me, I will and the practical degree to decide whether perfect.
5. Note:
1. The post request in this method is only used when the parameter is a pure string (for example, an XML string), and in other cases use the Postform form method.
2. The run<t> () method in this tool method is directly integrated with the "deserialize return string to object" operation (which supports JSON and XML, depending on the incoming data type), and the specific implementation method is given later, if you want to use now or no implementation method, This method can be removed (sorry, ha).
3, use the End method as run (), do not call the run () method, the request will not be executed.
4, this HTTP tool method using chain style, if you do not understand the place please find information or contact me.
Thank:
First of all, thanks to the great gods who provided me with the method of reflection, sending the post forms and files on HTTP, and so on, because I had a lot of information, I didn't list the data address and the author.
In this special thanks Blog Park Big God "he town Xi" to me to provide. NET technical aspects of teaching, thank you very much!!!
Ps:
OK, as a small white kitten First original blog is written here, this tool has not been fully developed (such as cookies, request encoding method, etc. only reserved, but not yet provided the Setup method), followed by the full version.
If the great God found that there are bugs or better advice, trouble contact me ha, help me to improve.
Http://demos.telerik.com/aspnet-ajax/dropdowntree/examples/overview/defaultcs.aspx can achieve all the functions in this.
HTTP Network Request