The Winform project calls the asp.net data interface.

Source: Internet
Author: User
A recent WPF project needs to be rewritten to an android project. The idea is to write a common interface in the asp.net project to facilitate data calling on other platforms. When I first came into contact with these things, it was completely confused. The most fundamental reason was that I did not understand a webpage on the website, why other projects could access it and obtain data. In asp.net projects with questions

A recent WPF project needs to be rewritten to an android project. The idea is to write a common interface in the asp.net project to facilitate data calling on other platforms. When I first came into contact with these things, it was completely confused. The most fundamental reason was that I did not understand a webpage on the website, why other projects could access it and obtain data. In asp.net projects with questions

A recent WPF project needs to be rewritten to an android project. The idea is to write a common interface in the asp.net project to facilitate data calling on other platforms. When I first came into contact with these things, it was completely confused. The most fundamental reason was that I did not understand a webpage on the website, why other projects could access it and obtain data. Write a simple data interface in the asp.net project with questions, and create a new winform project to access it directly. Knowledge points involved in this article include: how to compile a data interface in the asp.net project, how to use reflection to identify responses, and how to obtain interface data. Here we will only introduce how to use them, rather than the basic principles of using them. First, I am not familiar with the basic principles, and I am afraid to mislead future generations by writing them casually; second, if you elaborate on its basic principles, it will take a lot of time, but the time is limited. In the future, if the above two conditions are met, we will discuss them at the very bottom, because this is also an affirmation of our own progress. Start text.

Main content:

1. Write Data interfaces under the asp.net Project

2. Use reflection to identify the call Method

3. Create a New winform project to test the correctness of the interface

1. Write a simple interface under the asp.net Project

Compile a method to construct a json string Response.

private void ExamInfoLogin()        {            string aa = "8";            string bb = "9";            string roomName = Request.Form["RoomName"];            if (roomName == "806")            {                aa = "7";            }                                   StringBuilder jsonStringBuilder = new StringBuilder();            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 call Method

Assume that there are many methods on the aspx page, and only one of the methods needs to be called during use. In this example, the call method is selected using reflection.

Constants used during reflection:

Private const string PAGE_PATH_INFO = "/AppDataInterface/ExamLogin. aspx "; // page private const string ASSEMBLY_NAME =" OSCEWEB "; // Assembly private const string CLASS_NAME =" OSCEWEB. appDataInterface. examLogin "; // Class Name

Override OnInit method:

protected override void OnInit(EventArgs e)        {            string pathInfo = 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;                }            }        }

Add the following in the Page_Load method:

if (Request.Params["PATH_INFO"].StartsWith(PAGE_PATH_INFO + "/"))            {                Response.End();            }

3. Create a Winform project and access the data interface in asp.net.

Release the asp.net project. website: http: // 192.168.4.22: 8005

1) You do not need to transmit data to the data interface:

private void button1_Click(object sender, EventArgs e)        {            string strURL = "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 = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);            string responseText = streamReader.ReadToEnd();            streamReader.Close();            MessageBox.Show(responseText);        }

The obtained data is: {"UName": "8", "Password": "9 "}

2) transmit data to the data interface in post mode to obtain the interface data

private void button2_Click(object sender, EventArgs e)        {            string strURL = "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";            string param = "RoomName=806";            ASCIIEncoding encoding = new ASCIIEncoding ();            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 = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);            string responseText = streamReader.ReadToEnd();            streamReader.Close();            MessageBox.Show(responseText);        }

Data: {"UName": "7", "Password": "9 "}

4. Summary

According to the methods described above, the project can be completed, but it is still full of doubts about why it should be so. I always feel like I am in the bottom of my mind and I am very grateful to some experts who wish to give me some advice to my younger brother.

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.