标签:
经过了一番折腾,这个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; 7 8 // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍 9 10 namespace App 11 { 12 /// <summary> 13 /// 可用于自身或导航至 Frame 内部的空白页。 14 /// </summary> 15 public sealed partial class MainPage : Page 16 { 17 public MainPage() 18 { 19 this.InitializeComponent(); 20 21 this.NavigationCacheMode = NavigationCacheMode.Required; 22 } 23 24 /// <summary> 25 /// 在此页将要在 Frame 中显示时进行调用。 26 /// </summary> 27 /// <param name="e">描述如何访问此页的事件数据。 28 /// 此参数通常用于配置页。</param> 29 protected override void OnNavigatedTo(NavigationEventArgs e) 30 { 31 // TODO: 准备此处显示的页面。 32 33 // TODO: 如果您的应用程序包含多个页面,请确保 34 // 通过注册以下事件来处理硬件“后退”按钮: 35 // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。 36 // 如果使用由某些模板提供的 NavigationHelper, 37 // 则系统会为您处理该事件。 38 } 39 //点击登陆按钮 40 private async void button_Click(object sender, RoutedEventArgs e) 41 { 42 //这个uri是抓包获得的 43 var uri = "http://60.18.131.131:11080/newacademic/j_acegi_security_check"; 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; // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍 namespace App { /// <summary> /// 可用于自身或导航至 Frame 内部的空白页。 /// </summary> public sealed partial class BlankPage1 : Page { public BlankPage1() { this.InitializeComponent(); } /// <summary> /// 在此页将要在 Frame 中显示时进行调用。 /// </summary> /// <param name="e">描述如何访问此页的事件数据。 /// 此参数通常用于配置页。</param> 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的一个小尝试
标签:
原文地址:http://www.cnblogs.com/harrymusic/p/5398936.html