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

时间:2024-01-14 18:42:32

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

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

下面是源码:

 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 Windows.Web.Http;
namespace App
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required;
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
//点击登陆按钮
private async void button_Click(object sender, RoutedEventArgs e)
{
//这个uri是抓包获得的,就是那个带有post请求的uri
var uri = "xxx";
var values = new List<KeyValuePair<string, string>>();
//这个键值对也是抓包获得的,就是你的用户名和密码填上
values.Add(new KeyValuePair<string, string>("自己的用户名", "xxxxx"));
values.Add(new KeyValuePair<string, string>("自己的密码", "xxxxxx"));
string response = await App.httpClientHelper.Post(new Uri(uri), values);
//string responsetext = await response.Content.ReadAsStringAsync();
//这个uri是你想获得返回内容的uri
var _uri = "你想访问的uri";
string _response = await App.httpClientHelper.Get(new Uri(_uri));
Frame.Navigate(typeof(BlankPage1), _response);
}
}
}
<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的一个小尝试。