欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

windows phone 8.1教务在线客户端(后续)

程序员文章站 2022-06-22 12:23:42
经过了一番折腾,这个wp教务在线算是告一段落了,其实原理很简单,就是post方式访问登陆页面返回cookie,然后带着这个cookie用get方式继续访问你想要访问并取回内容的页面,而且httpclient会默认保存cookie的,这个关键我一开始就没搞清,以至于走了弯路,, ok,这个项目我只是登 ......

经过了一番折腾,这个wp教务在线算是告一段落了,其实原理很简单,就是post方式访问登陆页面返回cookie,然后带着这个cookie用get方式继续访问你想要访问并取回内容的页面,而且httpclient会默认保存cookie的,这个关键我一开始就没搞清,以至于走了弯路,,

ok,这个项目我只是登陆并且获取了我想要的html内容,至于解析html,可以用正则表达式,等以后有时间再研究吧

下面是源码:

 1 using Windows.UI.Xaml.Controls.Primitives;
 2 using Windows.UI.Xaml.Data;
 3 using Windows.UI.Xaml.Input;
 4 using Windows.UI.Xaml.Media;
 5 using Windows.UI.Xaml.Navigation;
 6 using Windows.Web.Http;
10 namespace App 11 { 15 public sealed partial class MainPage : Page 16 { 17 public MainPage() 18 { 19 this.InitializeComponent(); 20 21 this.NavigationCacheMode = NavigationCacheMode.Required; 22 } 23 29 protected override void OnNavigatedTo(NavigationEventArgs e) 30 { 38 } 39 //点击登陆按钮 40 private async void button_Click(object sender, RoutedEventArgs e) 41 { 42 //这个uri是抓包获得的,就是那个带有post请求的uri 43 var uri = "xxx"; 44 var values = new List<KeyValuePair<string, string>>();
//这个键值对也是抓包获得的,就是你的用户名和密码填上 45 values.Add(new KeyValuePair<string, string>("自己的用户名", "xxxxx")); 46 values.Add(new KeyValuePair<string, string>("自己的密码", "xxxxxx")); 47 string response = await App.httpClientHelper.Post(new Uri(uri), values); 48 //string responsetext = await response.Content.ReadAsStringAsync(); 49 //这个uri是你想获得返回内容的uri 50 var _uri = "你想访问的uri"; 51 string _response = await App.httpClientHelper.Get(new Uri(_uri)); 52 Frame.Navigate(typeof(BlankPage1), _response); 53 } 54 } 55 }
<Page
    x:Class="App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Button x:Name="button" Content="Login" Click="button_Click" HorizontalAlignment="Left" Margin="232,292,0,0" VerticalAlignment="Top"/>

        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Margin="201,138,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="166"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="43,138,0,0" TextWrapping="Wrap" Text="Name" FontSize="30" VerticalAlignment="Top" Height="39" Width="116"/>
        <TextBox x:Name="passwordBox" HorizontalAlignment="Left" Margin="201,218,0,0" VerticalAlignment="Top" Width="166"/>
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="43,218,0,0" TextWrapping="Wrap" Text="Password" FontSize="22" VerticalAlignment="Top" Height="39" Width="93"/>

    </Grid>
</Page>

 

这个是个空页面,里面放着所返回的html源码:

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;

namespace App
{
    public sealed partial class BlankPage1 : Page
    {
        public BlankPage1()
        {
            this.InitializeComponent();
        }

protected override void OnNavigatedTo(NavigationEventArgs e) { textBox.Text = e.Parameter.ToString(); } } }
<Page
    x:Class="App.BlankPage1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="55,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="-3.716,-0.387" Height="217" Width="301" BorderThickness="0.1"/>

    </Grid>
</Page>

 

这是自定义的httpClienthelper类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Web.Http;

namespace App
{
    public class HttpClientHelper
    {
        private HttpClient httpClient;

        public HttpClientHelper()
        {
            httpClient = new HttpClient();

        }

        public async Task<string> Post(Uri uri, IList<KeyValuePair<string, string>> postcontent)
        {
            string responseText = string.Empty;
            HttpResponseMessage response = await httpClient.PostAsync(uri, new HttpFormUrlEncodedContent(postcontent));
            responseText = await response.Content.ReadAsStringAsync();
            return responseText;
        }

        public async Task<string> Get(Uri uri)
        {
            string responseText = string.Empty;
            HttpResponseMessage response = await httpClient.GetAsync(uri);
            responseText = await response.Content.ReadAsStringAsync();
            return responseText;
        }
    }
}

一定别忘了在app.xaml.cs里实例化这个httpClientHelper实例:

public static HttpClientHelper httpClientHelper = new HttpClientHelper();

好了,这只是基本的原理展示,这是我学习windows phone的一个小尝试。