大佬教程收集整理的这篇文章主要介绍了(13):Silverlight 2 数据与通信之WebRequest,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<Grid BACkground="#46461F"> <Grid.RowDeFinitions> <RowDeFinition Height="40"></RowDeFinition> <RowDeFinition Height="*"></RowDeFinition> <RowDeFinition Height="40"></RowDeFinition> </Grid.RowDeFinitions> <Grid.columnDeFinitions> <columnDeFinition></columnDeFinition> </Grid.columnDeFinitions> <Border Grid.Row="0" Grid.column="0" CornerRadius="15" Width="240" Height="36" @H_434_43@margin="20 0 0 0" HorizontalAlignment="Left"> <TextBlock Text="书籍列表" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" @H_434_43@margin="20 0 0 0"></TextBlock> </Border> <ListBox x:Name="Books" Grid.Row="1" @H_434_43@margin="40 10 10 10" SELEctionChanged="Books_SELEctionChanged"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Name}" Height="32"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Border Grid.Row="2" Grid.column="0" CornerRadius="15" Width="240" Height="36" BACkground="Orange" @H_434_43@margin="20 0 0 0" HorizontalAlignment="Left"> <TextBlock x:Name="lblPrice" Text="价格:" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" @H_434_43@margin="20 0 0 0"></TextBlock> </Border> </Grid>编写httpHandler,注意我使用了context.request.Form["No"],在后面我们将使用Webrequest在requestReady方法中将数据写入请求流:
public class BookHandler : IhttpHandler { public static readonly String[] PriceList = new String[] { "66.00","78.30","56.50","28.80","77.00" }; public void Processrequest(httpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write(PriceList[Int32.Parse(context.request.Form["No"])]); } public bool IsReusable { get { return false; } } }
private void UserControl_Loaded(object sender,RoutedEventArgs E) { List<Book> books = new List<Book>() { new Book("Professional ASP.NET 3.5"),new Book("ASP.NET AJAX In Action"),new Book("Silverlight In Action"),new Book("ASP.NET 3.5 Unleashed"),new Book("Introducing Microsoft ASP.NET AJAX") }; Books.Itemssource = books; }
private String bookNo; void Books_SELEctionChanged(object sender,SELEctionChangedEventArgs E) { bookNo = Books.SELEctedIndex.ToString(); Uri endpoint = new Uri("http://localhost:49955/BookHandler.ashx"); Webrequest request = Webrequest.Create(endpoint); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.beginGetrequestStream(new AsyncCallBACk(requestReady),request); request.beginGetResponse(new AsyncCallBACk(ResponseReady),request); }
void requestReady(IAsyncResult asyncResult) { Webrequest request = asyncResult.AsyncState as Webrequest; Stream requestStream = request.EndGetrequestStream(asyncResult); using (StreamWriter writer = new StreamWriter(requestStream)) { writer.Write(String.Format("No={0}",bookNo)); writer.Flush(); } }
void ResponseReady(IAsyncResult asyncResult) { Webrequest request = asyncResult.AsyncState as Webrequest; WebResponse response = request.EndGetResponse(asyncResult); using (Stream responseStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream); lblPrice.Text = "价格:" + reader.ReadToEnd(); } }
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://www.voidcn.com/article/p-hvieqmus-wq.html
本文出自 51CTO.COM技术博客以上是大佬教程为你收集整理的(13):Silverlight 2 数据与通信之WebRequest全部内容,希望文章能够帮你解决(13):Silverlight 2 数据与通信之WebRequest所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。