WEB开发网
开发学院图形图像Flash How to: 使用http接收和发送简单的xml请求 阅读

How to: 使用http接收和发送简单的xml请求

 2008-10-04 11:37:36 来源:WEB开发网   
核心提示: 异步调用Web服务在按钮的单击事件中构造URI.string symbol = _textBox.GetAttribute("value");// The target web service must be on the same server as this Sil

异步调用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}rn", 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;
  }
}

上一页  1 2 3 

Tags:How to 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接