.NET Compact Framework下HttpWebRequest开发

本文介绍如何在WindowsMobile应用中,利用HttpWebRequest和HttpWebResponse从不提供WebService的ASP.NET WebForm获取数据。详细步骤包括分析所需提交的数据、获取ViewState、构造Post请求及解析返回的HTML。

当Windows Mobile程序需要使用Web Server的信息,一开始自然会想到使用WebService。可是有些Web Server不提供WebService,只是提供Http浏览功能,如果需要从Http网页中得到信息,可以使用HttpWebRequest和HttpWebResponse来完成,下面通过一个例子来讲述。

例如有一个使用ASP.net开发的燃料价格查询网站。

图1
输入燃料类型(Fuel Type)和邮政编码(Postcode)后可以查询出燃料价格,如下图。

图2

在Windows Mobile下需要开发一个应用程序,输入燃料类型和邮编,然后通过通过Web Server查询出价格信息,显示到Windows Mobile里面。由于该Web Server不提供WebService的服务,所以该Windows Mobile程序不能直接通过Webservice取出价格信息,可以通过HttpWebRequest来实现。关于HtppWebRequest可以参考下面两篇不错的文章。
使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
HttpWebRequest/Response in a nutshell - Part 1

总体开发分几步:第一步找出需要提交的数据;第二步访问该页面,取出ViewState;第三步组成Post的数据并发送请求;第四步分析HTML并显示结果。

找出需要提交的数据

找出需要提交的数据最常用的工具是HttpWatch,可是这个工具免费的basic版本只是支持google,microsoft等网站,所以我使用了一个Firefox免费插件HttpFox,事实证明该工具满足我的需求。
先安装HttpFox,然后点击HttpFox的"Start"启动。打开要提交的页面,例子为 http://localhost/HttpWebRequest/Default.aspx,点击页面中的"Search"按钮。查看HttpFox的"Post Data"Tab页,能找出所有需要提交的数据,如下图

需要提交的POST包括__VIEWSTATE, __EVENTVALIDATION, DropDownListFuelType, TextBoxPostCode以及 ButtonSearchPostcode


图3

取出ViewState

进行ASP.net的webform开发,关键是了解页面的生命周期,其中一个关键点是ViewState,后台把所需要的信息存放到一个叫做__VIEWSTATE的隐藏input框里,这__VIEWSTATE会往服务器回传服务端控件信息(Server controls),下面就是一个ViewState的例子。

< div >
 
< input type = " hidden "  name = " __VIEWSTATE "  id = " __VIEWSTATE "  value = " /wEPDwULLTE5NjAwMTc3OTQPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BTRDbGljazogRnVlbD1bRGllc2VsXSwgUG9zdGNvZGVyPVsyMDE0XSwgUHJpY2VzPVsxLjVdZGRkPwaT5ufoa4sSWz2jkkc4+1mjzgA= "   />
</ div >
< div >
 
< input type = " hidden "  name = " __EVENTVALIDATION "  id = " __EVENTVALIDATION "  value = " /wEWCALvtKiPBwLHjuPuBALEjuPuBALFjuPuBALCjuPuBALDjuPuBAKJr8zPBQLj1NuXC4Mp8caGaDkuTJ50BTH0riQ7xWbK "   />
</ div >

 

在CF.NET下进行HttpWebRequest开发,如果服务端是ASP.net的程序,需要处理ViewState。从图3上看,除了__VIEWSTATE还需要处理__EVENTVALIDATION,其实很简单,通过HttpFox看那个需要取那个。

// Get the ViewState and EventValidation
string  URI  =   " http://192.168.1.149/HttpWebRequest/Default.aspx " ;
HttpWebRequest request 
=  WebRequest.Create(URI)  as  HttpWebRequest;
request.Method 
=   " GET " ;
request.KeepAlive 
=   false ;

// Get the response
HttpWebResponse response  =  request.GetResponse()  as  HttpWebResponse;
System.IO.Stream responseStream 
=  response.GetResponseStream();
System.IO.StreamReader reader 
=   new  System.IO.StreamReader(responseStream, Encoding.UTF8);
string  srcString  =  reader.ReadToEnd();

// Get the ViewState
string  viewStateFlag  =   " id=\ " __VIEWSTATE\ "  value=\ "" ;
int  i  =  srcString.IndexOf(viewStateFlag)  +  viewStateFlag.Length;
int  j  =  srcString.IndexOf( " \ "" , i);
string  viewState  =  srcString.Substring(i, j  -  i);

// Get the ViewState
string  EventValidationFlag  =   " id=\ " __EVENTVALIDATION\ "  value=\ "" ;
=  srcString.IndexOf(EventValidationFlag)  +  EventValidationFlag.Length;
=  srcString.IndexOf( " \ "" , i);
string  eventValidation  =  srcString.Substring(i, j  -  i);

 

取ViewState要先访问提交前的网页,使用HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;生成一个HttpWebRequest的对象,参数为Web服务器的地址,提交方式为"GET",其实这个方式也可以通过HttpFox看到的,如下图:

图4
先启动HttpFox,然后输入网址,看第一行知道这是一个"GET"操作。
HttpWebResponse response = request.GetResponse() as HttpWebResponse;为向Web服务器发送请求,并得到返回信息。返回的信息其实就是图4里的"Content"Tab页,也就是HTML信息,里面包含我们需要的ViewState。

组成Post的数据并发送请求

处理结果得到__VIEWSTATE和__EVENTVALIDATION,然后可以组成提交操作还需要组成Post的数据,如下代码

// Compose the URL
viewState  =  Uri.EscapeDataString(viewState);
eventValidation 
=  Uri.EscapeDataString(eventValidation);

string  formatString  =   " __VIEWSTATE={0}&__EVENTVALIDATION={1}&DropDownListFuelType={2}&TextBoxPostCode={3}&ButtonSearchPostcode=Search " ;
string  postString  =   string .Format(formatString, viewState, eventValidation, fuelType, postCode);

// Change to byte[]
byte [] postData  =  Encoding.ASCII.GetBytes(postString);

// Compose the new request
request  =  WebRequest.Create(URI)  as  HttpWebRequest;
request.Method 
=   " POST " ;
request.KeepAlive 
=   false ;
request.ContentType 
=   " application/x-www-form-urlencoded " ;
request.ContentLength 
=  postData.Length;

System.IO.Stream outputStream 
=  request.GetRequestStream();
outputStream.Write(postData, 
0 , postData.Length);
outputStream.Close();

// Get the new response
response  =  request.GetResponse()  as  HttpWebResponse;
responseStream 
=  response.GetResponseStream();
reader 
=   new  System.IO.StreamReader(responseStream);
srcString 
=  reader.ReadToEnd();
return  srcString;

 

Post的数据需要使用Uri.EscapeDataString格式化,如果提交的数据包含中文,一定要进行这一步操作,否则会出错。

string  formatString  =   " __VIEWSTATE={0}&__EVENTVALIDATION={1}&DropDownListFuelType={2}&TextBoxPostCode={3}&ButtonSearchPostcode=Search " ;
string  postString  =   string .Format(formatString, viewState, eventValidation, fuelType, postCode);

就是组成Post的内存,所有内容来自于HttpFox的分析,见图3,需要什么组成什么。
然后再使用HttpWebRequest和HttpWebResponse提交,得到返回结果,这个结果包含了我们需要的价格信息。

分析HTML并显示结果

得到结果后,需要分析HTML并把我们需要的信息显示出来,在这一例子里,使用简单的字符串分析,从LabelResult里得到价格信息。

int  i  =  srcString.IndexOf( " Price=[ " );
int  j  =  srcString.IndexOf( " </span> " );
labelPrice.Text 
=  srcString.Substring(i, j  -  i);

 
这里只是一个简单的例子,在实际运用中可以使用正则表达来分析,或者使用HTML分析器来解释。


到目前为止已经讲述了一个简单的HttpWebRequest运用。源代码见下面。运行该代码,需要修改IP地址,这里hardcode了Server的地址。
如果使用Emulator进行测试,可以参考在Windows Mobile Emulator建立网络连接

源代码 HttpWebRequestTester.rar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值