WEB开发网
开发学院图形图像Flash [玩转Silverlight]第一回:基础篇,品尝与HttpHan... 阅读

[玩转Silverlight]第一回:基础篇,品尝与HttpHandler通信

 2009-02-06 11:54:33 来源:WEB开发网   
核心提示: public class UserHandler : IHttpHandler, IRequiresSessionState{public void ProcessRequest(HttpContext context){string name = context.Request.Quer

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);
}

上一页  1 2 3 4 5 6  下一页

Tags:Silverlight 第一回 基础

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接