首页 技术 正文
技术 2022年11月20日
0 收藏 369 点赞 5,100 浏览 3229 个字

本人是个大二学生,由于学校的教务在线一直没出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,希望有经验的园友指导一下

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905