howto使用http接收和发送简单的xml请求

来源:互联网 发布:ipad无法更新软件 编辑:程序博客网 时间:2024/05/21 14:44
howto使用http接收和发送简单的xml请求
介绍:
这个快速入门示例演示了如何借助http来发送和接收“简单的”[l1] xml请求。
(有时候我们也指POX:Plain Old XML)
POX包括一个没有信封的soap xml。(解释一下soap:简单对象访问协议,simple object Access protocol.这个协议规定了一个soap包含有外层的信封,和一个soap头,和soap 体。这个外面的信封主要是用于在企业级应用中涉及到的分布式服务的事务,信任,安全,这些机制由信封来完成扩展功能。)
POX 能被许多种类的客户端接收和发送,包括对Soap协议没有内在支持的浏览器。POX对于通过HTTP来交换数据却不需要高级SOAP能力和WS-*协议的服务来讲是个合适的选择。(例如,非http数据传输,其他的请求/响应模型的消息格式,基于消息的安全,可靠性,事务等)
POX 是用一个新的类来发送请求:BrowserHttpWebRequest,通过这个类:HttpWebResponse来接收请求。
注意:BrowserHttpWebRequest目前并不支持跨域调用,目前BrowserHttpWebRequest的版本只能调用与Silverlight程序运行在同一台机器上的Asp.NET Ajax Web Service服务。
 
译者评价:如果调用服务不可以跨域,那么将会对其发展有巨大的限制,因为不可能全世界所有你想调用的服务都跟你在一台服务器上。比如航班即时信息,天气预报,你就需要实现跨域访问才行。但是这个并不难实现,通过asp.net ajax调用的服务在服务器端本身也是也是一个代理,调用了外部的服务,即通过服务调用服务的形式,绕道走一次。
 
使用POX需要一下的步骤:
创建一个web 服务。
创建一个银光项目。
加入同步连接web服务的托管代码。
加入异步连接web服务的托管代码。
 
先决条件:
*    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.
 
 
 
 
1 创建一个服务:
如果对如何创建一个服务有疑问,可以访问: http://msdn2.microsoft.com/en-us/library/41e7c6d1-dcb9-4423-b3af-60b792edb28b
 
2 创建一个银光项目:
如果对如何创建一个银光项目有疑问,可以访问: http://myspace.silverlight.cn/Quickstart1.1_CH/Start/CreateProject.aspx
 
3.1 同步调用web服务:
点击按钮的事件执行的就是构造一个请求资源,代码如下:
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);
 
3,2 调用这个服务:
_status.Text = String.Format("Calling {0}/n/r", webServiceUri.ToString());
_request = new BrowserHttpWebRequest(webServiceUri);    
 
3.3        处理响应:
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();
 
代码主要是对响应流的解析成xml流,然后输出。
 
4         异步调用web服务:
4.1 构造服务的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=" +
 
            调用服务:
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);
 
4.3        处理请求:
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;
   }
}
 
这个异步的步骤是.net中的典型异步模型,客户端在服务器端注册一个异步方法传递相关参数后,线程立即返回。等到服务器端处理完毕,就回调客户端刚才在服务器端注册的那个回调方法,并传递相关参数,很容易看懂。
原文地址:http://silverlight.cn/blogs/xiang/archive/2007/06/19/howto-http-xml.aspx