经过了一番折腾,这个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&g