最近一個WPF項目需要改寫成android項目,思路是在asp.net項目中編寫一個通用接口,便于其它平臺下調用數據。剛接觸到這些東西的時候完全是一頭霧水,最根本的原因是不明白網站中的一個網頁,為什么其它項目就可以訪問它,并獲取數據。帶著疑問在asp.net項目編寫一個簡單的數據接口,并新建一個小winform項目直接訪問它。本文涉及到的知識點有:在asp.net項目中如何編寫一個數據接口;使用反射辨別響應的方法;以及如何獲取接口的數據。這里僅僅是介紹如何使用它們,而不講述使用它們的基本原理,一是本人道行淺薄對基本原理不了解,害怕隨便書寫誤導后人;二是如果闡述其基本原理,勢必需要花費大量時間,奈何時間有限。將來如果上述兩個條件滿足,必會在最下面做出論述,因為這對自己的進步也是一個肯定。閑話少說,開始正文。
主要內容:
1、asp.net項目下編寫數據接口
2、使用反射分辨調用方法
3、新建一個winform項目測試接口的正確性
1、在asp.net項目下編寫一簡單接口
編寫一個方法,構造一個json字符串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、使用反射選取調用方法
假設在aspx頁面中有很多方法,而在使用過程中往往僅需要調用其中的某一個方法,此處用反射選取調用方法。
反射過程中使用的常量:
private const string PAGE_PATH_INFO = "/AppDataInterface/ExamLogin.aspx";//頁面 private const string ASSEMBLY_NAME = "OSCEWEB";//程序集 private const string CLASS_NAME = "OSCEWEB.AppDataInterface.ExamLogin";//類名
重寫OnInit方法:
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; } } }
在Page_Load方法中添加:
if (Request.Params["PATH_INFO"].StartsWith(PAGE_PATH_INFO + "/")) { Response.End(); }
3、新建一Winform項目,訪問asp.net中數據接口
發布asp.net項目,網址:http://192.168.4.22:8005
1)無需向數據接口傳遞數據:
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); }
得到的數據是:{"UName":"8","Password":"9"}
2)以post方式向數據接口傳遞數據,獲取接口數據
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); }
得到的數據:{"UName":"7","Password":"9"}
4、總結
按照上述介紹的一些方法確實能完成項目,但是對其為什么該如此還是充滿疑惑,總感覺心中無底、戰戰兢兢,希望有高手可以對小弟指點一二,不勝感激。
新聞熱點
疑難解答