How to: 使用http接收和发送简单的xml请求
2008-10-04 11:37:36 来源:WEB开发网创建一个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}nr", 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();
更多精彩
赞助商链接