[玩转Silverlight]第一回:基础篇,品尝与HttpHandler通信
2009-02-06 11:54:33 来源:WEB开发网注意,以QueryString进行数据传递并非一成不变的方式,不同浏览器对于Url的长度和编码都有不同的规则,例如大数据量通信情况下应该考虑以UploadString方式进行,通过Http Post方法向Handler发送请求,我们将在下回中讲述序列化主题时进行相关的讨论和应用示例。
WebRequest/WebResponse方式
以WebRequest方式进行数据通信,实现相同的请求操作,需要如下的实现:
private void GetResultByWebRequest(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);
WebRequest request = WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);
}
private void RequestReady(IAsyncResult ar)
{
WebRequest request = ar.AsyncState as WebRequest;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(request.EndGetRequestStream(ar)))
{
sw.Write("Post data to server.");
}
request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
}
private void ResponseReady(IAsyncResult ar)
{
WebRequest request = ar.AsyncState as WebRequest;
using (WebResponse response = request.EndGetResponse(ar))
{
using (System.IO.Stream stream = response.GetResponseStream())
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
Dispatcher.BeginInvoke((InvokeDelegate)BindAndNext, reader.ReadToEnd());
}
}
}
}
private void BindAndNext(string result)
{
if (!string.IsNullOrEmpty(result))
{
this.result.Text = result;
//Call another WCF Service continuously
}
else
{
MessageBox.Show("Error");
}
}
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...
更多精彩
赞助商链接