码迷,mamicode.com
首页 > Windows程序 > 详细

Windows phone8.1教务在线客户端

时间:2015-12-05 17:43:39      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

本人是个大二学生,由于学校的教务在线一直没出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;
using System.Text.RegularExpressions;
public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }
        private async void button_Click(object sender, RoutedEventArgs e)
        {
            //这句postdata的拼写可能有错误,考虑用键值对的方式
            string loginstr = "j_username=" + textBox1.Text + "&j_password=" + passwordBox.ToString();

            CookieContainer cookie = await GetCookie(loginstr,"http://60.18.131.131:11080/newacademic/common/security/login.jsp");
            string content = await GetContent(cookie, "http://60.18.131.131:11080/newacademic/frameset.jsp");
            Frame.Navigate(typeof(BlankPage1), content);
            textBox2.Text = content;
        }

        public async Task<CookieContainer> GetCookie (string postString,string postUrl)
        {
            CookieContainer cookie = new CookieContainer();
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postUrl);
            //下面这个请求头的信息不知如何改进啊,反正有什么我就把它添上了
            httpRequest.CookieContainer = cookie;//设置cookie
            httpRequest.Method = "POST";//POST提交
            httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString);
            Stream stream = await httpRequest.GetRequestStreamAsync();
            stream.Write(bytes, 0, bytes.Length);//以上是post数据的写入,不知还有什么疏漏之处          
            HttpWebResponse httpResponse =(HttpWebResponse)await httpRequest.GetResponseAsync();
            return cookie;
        }

        public async Task<string> GetContent(CookieContainer cookie,string url)
        {
            string content;
            HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpRequest.CookieContainer = cookie;
            httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)await httpRequest.GetResponseAsync();
            using(Stream responseStream = httpResponse.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream,System.Text.Encoding.UTF8))
                {
                    content = sr.ReadToEnd();
                }
            }
            return content;
        }
    }
}

逻辑上大致是先模拟登陆,然后拿到cookie,再带着这个cookie访问登陆上的网页,(然后通过解析html获取自己想要的内容,这部分还没做)

不过并不能取到所返回的content,希望有经验的园友指导一下

Windows phone8.1教务在线客户端

标签:

原文地址:http://www.cnblogs.com/harrymusic/p/5021860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!