標籤:
本人是個大二學生,由於學校的教務線上一直沒出windows phone的教務線上,而且本身也對wp開發感興趣,所以就嘗試著開發一下
由於沒有系統的學習,只能在摸索中前進,這背後的原理很簡單,可不容易實現,網上也沒合適的教程,下面是本人嘗試著寫登入頁面的代碼,
不過還是有些文法上的問題,希望有貴人相助
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Runtime.InteropServices.WindowsRuntime;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;using System.Collections.Specialized;using System.Text;using System.Xml.Linq;using System.Threading.Tasks;// “空白頁”項目範本在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介紹namespace App3{ /// <summary> /// 可用於自身或導航至 Frame 內部的空白頁。 /// </summary> public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; } //這是一個按鈕的事件,點擊時會登入教務線上,並跳轉到另一個頁面上顯示課程資訊 private void button_Click(object sender, RoutedEventArgs e) { //這是想調用下面的方法,不知對不對,希望園友相助 AsyncCallback callBack = new AsyncCallback(ReadCallback); } private void ReadCallback(IAsyncResult asynchronousResult) { //這是登入頁面的uri string loginuri = "http://60.18.131.131:11080/newacademic/common/security/login.jsp"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginuri); //設定post的一些資訊 request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Credentials = new NetworkCredential(textBox1.Text, passwordBox.ToString()); HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); //擷取cookie CookieCollection cookies = response.Cookies; //這是已經登入的uri string posturl = "http://60.18.131.131:11080/newacademic/j_acegi_security_check"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(posturl); //req.CookieContainer = new CookieContainer(); //req.CookieContainer.Add(posturl,response.Cookies); //cookie是如何加入到要求標頭裡去的還有待探討 req.Headers["Cookie"] = cookies.ToString(); //這裡我拿到返回的stream Stream stream = req.EndGetRequestStream(asynchronousResult); StreamReader sr = new StreamReader(stream); string content = sr.ReadToEnd(); //把stream放置到另一個頁面裡 Frame.Navigate(typeof(BlankPage1),content); }
邏輯上大致是先類比登陸,然後拿到cookie,再帶著這個cookie訪問登陸上的網頁,(然後通過解析html擷取自己想要的內容,這部分還沒做)
不過還有一些問題,希望有經驗的園友指導一下
關於windows phone教務線上用戶端