WinForm Project calls the ASP. NET Data Interface

Source: Internet
Author: User

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 know a Web page in your 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, however, we describe how to use them, rather than the rationale for using them. First, I daoxing shallow to the basic principles do not understand. Be afraid to write misleading posterity, and second, to explain its basic principles. The potential must take a lot of time, but time is limited. In the future, if the above two conditions are satisfied, it will be discussed in the most of the following, because this is also a positive for their progress. Talk 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 to construct 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

If there are very many methods in the ASPX page, it is often only necessary to invoke one of the methods in the process. The call method is selected here 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 and access the data interface in ASP.

Advertise the ASP. Web project: 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) The data is passed to the data interface in post mode. 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

In accordance with some of the methods described above, the project can indeed be completed. But there is a lot of doubt as to why it should be so. Always feel the heart bottomless, trembling. I hope to have a master to the younger brother pointing twos, greatly appreciated.

WinForm Project calls the ASP. NET Data Interface

Related Article

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.