A recent WPF project needs to be rewritten as an Android project, with the idea of writing a common interface in an ASP to make it easier to invoke data under other platforms. The first thing that comes into contact with these things is confused, the most fundamental reason is that you don't understand a Web page in the site, why other projects can access it, and get data. Write a simple data interface in the ASP. NET project and create a new small WinForm project to access it directly. The knowledge points covered in this article are: How to write a data interface in an ASP., how to use reflection to discern the response, and how to get the data of the interface. Here is just a description of how to use them, not the basic principle of using them, one is I daoxing shallow to the basic principles do not understand, afraid of casually writing misleading posterity, second, if the rationale, it will take a lot of time, but time is limited. In the future if the above two conditions meet, will be at the bottom of the discussion, because it is a positive for their own progress. Gossip less, start the text.
Main content:
1. Write data interface under ASP.
2. Call method Using reflection resolution
3, a new WinForm Project test interface correctness
1. Write a simple interface under the ASP.
Write a method that constructs a JSON string response.
Private voidExaminfologin () {stringAA ="8"; stringBB ="9"; stringRoomname = request.form["Roomname"]; if(Roomname = ="806") {AA="7"; } StringBuilder Jsonstringbuilder=NewStringBuilder (); Jsonstringbuilder.append ("{"); Jsonstringbuilder.append ("\ "uname\": \ ""). Append (AA). Append ("\","); Jsonstringbuilder.append ("\ "password\": \ ""). Append (BB). Append ("\""); Jsonstringbuilder.append ("}"); Response.Write (Jsonstringbuilder.tostring ()); }
2. Use reflection to select the Calling method
Suppose there are many methods in an ASPX page, and in the process of using it, it is often only necessary to invoke one of these methods, where the calling method is selected with reflection.
Constants used in the reflection process:
Private Const stringPage_path_info ="/appdatainterface/examlogin.aspx";//page Private Const stringAssembly_name ="Osceweb";// Assembly Private Const stringClass_name ="Osceweb. Appdatainterface.examlogin";//class name
Overriding the OnInit method:
protected Override voidOnInit (EventArgs e) {stringPathInfo = request.params["Path_info"]; if(Pathinfo.startswith (Page_path_info +"/")) { string[] NameList = pathinfo.substring (page_path_info. Length +1). Split ('/'); if(Namelist.length <1) {Response.End (); return; } Try{Assembly Assembly=Assembly.Load (assembly_name); Type type=Assembly. GetType (class_name); MethodInfo Method= Type. GetMethod (namelist[0], System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance); Method. Invoke ( This,NULL); } Catch(Exception ex) {Response.End (); return; } } }
In the Page_Load method, add:
if (request.params["path_info" "/")) { response.end (); }
3. Create a new WinForm project to access the data interface in ASP.
Publish an ASP. Web project at: http://192.168.4.22:8005
1) There is no need to pass data to the data interface:
Private voidButton1_Click (Objectsender, EventArgs e) { stringstrURL ="Http://192.168.4.22:8005/AppDataInterface/ExamLogin.aspx/ExamInfoLogin"; Request=(System.Net.HttpWebRequest) webrequest.create (strURL); Response=(System. Net.httpwebresponse) Request. GetResponse (); System.IO.StreamReader StreamReader=NewSystem.IO.StreamReader (response. GetResponseStream (), Encoding.UTF8); stringResponseText =Streamreader.readtoend (); Streamreader.close (); MessageBox.Show (ResponseText); }
The data obtained are: {"UName": "8", "Password": "9"}
2) Send data to Data interface by post, get interface data
Private voidButton2_Click (Objectsender, EventArgs e) { stringstrURL ="Http://192.168.4.22:8005/AppDataInterface/ExamLogin.aspx/ExamInfoLogin"; Request=(System.Net.HttpWebRequest) webrequest.create (strURL); Request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; stringparam ="roomname=806"; ASCIIEncoding encoding=Newasciiencoding (); byte[] data =encoding. GetBytes (param); Request. ContentLength=data. Length; System.IO.Stream Stream=request. GetRequestStream (); Stream. Write (data,0, data. Length); Stream. Close (); Response=(System.Net.HttpWebResponse) request. GetResponse (); System.IO.StreamReader StreamReader=NewSystem.IO.StreamReader (response. GetResponseStream (), Encoding.UTF8); stringResponseText =Streamreader.readtoend (); Streamreader.close (); MessageBox.Show (ResponseText); }
Data obtained: {"UName": "7", "Password": "9"}
4. Summary
According to some of the methods described above can indeed complete the project, but why it is so still full of doubts, always feel the heart bottomless, trembling, hoping to have a master can be the younger brother pointing twos, greatly appreciated.
WinForm Project calls the ASP. NET Data Interface