这个快速入门示例演示了如何借助http来发送和接收“简单的”xml请求。(有时候我们也指POX:Plain Old XML)
POX包括一个没有信封的soap xml。(解释一下soap:简单对象访问协议,simple object Access protocol.这个协议规定了一个soap包含有外层的信封,和一个soap头,和soap 体。这个外面的信封主要是用于在企业级应用中涉及到的分布式服务的事务,信任,安全,这些机制由信封来完成扩展功能。)包括对Soap协议没有内在支持的浏览器。POX对于通过HTTP来交换数据却不需要高级SOAP能力和WS-*协议的服务来讲是个合适的选择。(例如,非http数据传输,其他的请求/响应模型的消息格式,基于消息的安全,可靠性,事务等)
POX 是用一个新的类BrowserHttpWebRequest 来发送请求,通过 HttpWebResponse 来接受响应.
注意
BrowserHttpWebRequest 目前并不支持跨域调用. 目前BrowserHttpWebRequest 的版本只能调用与Silverlight程序运行在同一台机器上的Asp.NET Ajax Web Service服务。
Run View 使用POX需要以下的步骤:
创建一个web 服务.
创建一个Silverlight项目.
加入同步连接web服务的托管代码.
加入异步连接web服务的托管代码.
系统需求 (available from the Silverlight download site):
Microsoft Silverlight 1.1 Alpha.
Microsoft Visual Studio Code Name "Orcas" Beta 1.
Microsoft Silverlight Tools Alpha for Visual Studio Code Name "Orcas" Beta 1.
供Silverlight程序访问的Web服务. 点击下面的按钮查看示例:
Run View 创建一个Web服务 通过下面的方法可创建Web服务 Walkthrough: 通过ASP.NET创建一个基本的 XML Web Service.
创建一个Silverlight项目 参见 How to: 创建一个Silverlight项目.
同步调用web服务 在按钮的单击事件中构造URI.
string symbol = _textBox.GetAttribute("value");
// The target web service must be on the same server as this Silverlight application.
string serverUri = HtmlPage.DocumentUri.ToString();
int thisApp = serverUri.IndexOf("/Silverlight.net");
// Create the web service Url with this server as its base.
serverUri = serverUri.Substring(0, thisApp) + "/Silverlight.net.webservice/cs/WebService.asmx";
// Pass the input string to the EchoInput method in the web service.
System.Uri webServiceUri = new System.Uri(serverUri + "/EchoInput?input=" + symbol);
调用Web服务
_status.Text = String.Format("Calling {0}\n\r", webServiceUri.ToString());
_request = new BrowserHttpWebRequest(webServiceUri);
处理响应.
HttpWebResponse response = (HttpWebResponse)_request.GetResponse();
// Read response
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string RawResponse = responseReader.ReadToEnd();
XmlReader xr = XmlReader.Create(new StringReader(RawResponse));
// Get main element
xr.ReadToFollowing("string");
// Get following text node
xr.Read();
_result.Text = "XmlReader parsed \"" + xr.Value + "\"";
xr.Close();
_request.Close();
异步调用Web服务
在按钮的单击事件中构造URI.
string symbol = _textBox.GetAttribute("value");
// The target web service must be on the same server as this Silverlight application.
string serverUri = HtmlPage.DocumentUri.ToString();
int thisApp = serverUri.IndexOf("/Silverlight.net");
// Create the web service Url with this server as its base.
serverUri = serverUri.Substring(0, thisApp) + "/Silverlight.net.webservice/cs/WebService.asmx";
// Pass the input string to the EchoInput method in the web service.
System.Uri webServiceUri = new System.Uri(serverUri + "/EchoInput?input=" + symbol);
调用Web服务.
status.Text = String.Format("Calling {0}\r\n", webServiceUri.ToString());
_request = new BrowserHttpWebRequest(webServiceUri);
// Include the request object as the IAsyncResult.AsyncState property.
IAsyncResult iar = _request.BeginGetResponse(new AsyncCallback(OnResponseDownload),
_request);
处理响应.
public void OnResponseDownload(IAsyncResult iar)
{
string parsedtext = string.Empty;
try
{
// Use the request object from the IAsyncResult.AsyncState property to obtain the response.
HttpWebResponse response = ((HttpWebRequest)iar.AsyncState).EndGetResponse(iar);
if (response.StatusCode != HttpStatusCode.OK)
throw new ApplicationException("HttpStatusCode " +
response.StatusCode.ToString() + " was returned.");
// Read response
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string rawResponse = responseReader.ReadToEnd();
response.Close();
XmlReader xr = XmlReader.Create(new StringReader(rawResponse));
// Get main element
xr.ReadToFollowing("string");
// Get following text node
xr.Read();
_result.Text = "XmlReader parsed \"" + xr.Value + "\"";
xr.Close();
responseReader.Close();
}
catch (Exception ex)
{
_status.Text = ex.Message;
}
}
原文地址