[玩转Silverlight]第一回:基础篇,品尝与HttpHandler通信
2009-02-06 11:54:33 来源:WEB开发网public class UserHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string name = context.Request.QueryString["name"];
string pwd = context.Request.QueryString["pwd"];
string result = ValidateUser(name, pwd);
context.Response.ContentType = "application/x-www-form-urlencoded";
if (string.IsNullOrEmpty(result))
{
context.Response.Write("Null");
}
else
{
context.Response.Write(result);
}
}
private string ValidateUser(string name, string pwd)
{
if (name.ToLower().CompareTo("anytao") == 0 && pwd.ToLower().CompareTo("123") == 0)
{
return name + " is a authorized user.";
}
else
{
return name + " is invalid user.";
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
下面继续数据通信部分的实现。
WebClient方式
对于这种应用WebClient提供了最简单的理想方式,可以选择相应的既存方法处理不同数据通信的需求,例如在本例中我们将通过DownloadString下载从handler处理之后的响应:
private void GetResultByWebClient(string name, string pwd)
{
string absolutePath = HtmlPage.Document.DocumentUri.AbsoluteUri;
string address = absolutePath.Substring(0, absolutePath.LastIndexOf('/'))
+ "/Handler/UserHandler.ashx?name=" + name + "&pwd=" + pwd;
Uri uri = new Uri(address);
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (null == e.Error)
{
result.Text = e.Result;
//Call other service here to continue operation
}
else
{
MessageBox.Show(e.Error.Message);
}
};
client.DownloadStringAsync(uri);
}
Tags:Silverlight 第一回 基础
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››基础推广如何完成网站的优化升级(一)
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
- ››silverlight2 游戏 1 你能坚持多少秒
- ››Silverlight开发实践--PicZoomShow
- ››Silverlight自定义控件开发 - 令人懊恼的OnApplyT...
更多精彩
赞助商链接