网络和通信 - Silverlight 中的 HTTP 通信和安全
2011-03-20 11:13
232 查看
Silverlight 支持几种使用 HTTP/HTTPS 的方案。虽然可以使用多种方式和技术执行 HTTP 调用,但是下表描述的是针对这些 HTTP
通信方案的建议方法
View Code
通信方案的建议方法
View Code
SynchronizationContext syncContext; private void Button_Click(object sender, RoutedEventArgs e) { // Grab SynchronizationContext while on UI Thread syncContext = SynchronizationContext.Current; // Create request HttpWebRequest request = WebRequest.Create(new Uri("http://blogs.contoso.com/post-create?blogID=1234", UriKind.Absolute)) as HttpWebRequest; request.Method = "POST"; // Make async call for request stream. Callback will be called on a background thread. IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), request); } string statusString; private void RequestStreamCallback(IAsyncResult ar) { HttpWebRequest request = ar.AsyncState as HttpWebRequest; request.ContentType = "application/atom+xml"; Stream requestStream = request.EndGetRequestStream(ar); StreamWriter streamWriter = new StreamWriter(requestStream); streamWriter.Write("<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title type='text'>New Restaurant</title>" + "<content type='xhtml'>" + " <div xmlns='http://www.w3.org/1999/xhtml'>" + " <p>There is a new Thai restaurant in town!</p>" + " <p>I ate there last night and it was <b>fabulous</b>.</p>" + " <p>Make sure and check it out!</p>" + " </div>" + " </content>" + "<author>" + " <name>Pilar Ackerman</name>" + " <email>packerman@contoso.com</email>" + " </author>" + "</entry>"); // Close the stream. streamWriter.Close(); // Make async call for response. Callback will be called on a background thread. request.BeginGetResponse(new AsyncCallback(ResponseCallback), request); } private void ResponseCallback(IAsyncResult ar) { HttpWebRequest request = ar.AsyncState as HttpWebRequest; WebResponse response = null; try { response = request.EndGetResponse(ar); } catch (WebException we) { statusString = we.Status.ToString(); } catch (SecurityException se) { statusString = se.Message; if (statusString == "") statusString = se.InnerException.Message; } // Invoke onto UI thread syncContext.Post(ExtractResponse, response); } private void ExtractResponse(object state) { HttpWebResponse response = state as HttpWebResponse; if (response != null && response.StatusCode == HttpStatusCode.OK) { StreamReader responseReader = new StreamReader(response.GetResponseStream()); tb1.Text = response.StatusCode.ToString() + " Response: " + responseReader.ReadToEnd(); } else tb1.Text = "Post failed: " + statusString; }
相关文章推荐
- \t\t【总结】SilverLight与Wcf HTTP 通信 安全
- 《Android开发卷——HTTP网络通信,HTTP网络连接》
- 由http暗藏通道看网络安全
- 企业信息与网络通信安全(8)调查问卷
- Android网络通信框架LiteHttp:开篇简介和教程大纲
- Android中网络通信基于HTTP、Socket
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和Browse
- silverlight系列(XML操作、HTTP通信、WebRequest通信)
- Android网络编程之Http通信
- 20145240 《信息安全系统设计基础》实验五 网络通信
- Android 网络编程之 Http 通信
- HTTP协议 与网络通信
- 20145310《信息安全系统设计基础》实验五 网络通信
- Android HTTP网络通信
- android的网络通信之HttpClient
- <1>移动设备的网络通信 http /socket
- Android网络编程之Http通信(cmwap处理)
- 网络通信第三课 HTTP请求数据携带Json数据格式分析
- iOS学习笔记(八)——iOS网络通信http之NSURLConnection