栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

客户端发送SOAP请求并接收响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

客户端发送SOAP请求并接收响应

我通常使用另一种方法来做同样的事情

using System.Xml;using System.Net;using System.IO;public static void CallWebService(){    var _url = "http://xxxxxxxxx/Service1.asmx";    var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";    Xmldocument soapEnvelopeXml = CreateSoapEnvelope();    HttpWebRequest webRequest = CreateWebRequest(_url, _action);    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);    // begin async call to web request.    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);    // suspend this thread until call is complete. You might want to    // do something usefull here like update your UI.    asyncResult.AsyncWaitHandle.WaitOne();    // get the response from the completed web request.    string soapResult;    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))    {        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))        { soapResult = rd.ReadToEnd();        }        Console.Write(soapResult); }}private static HttpWebRequest CreateWebRequest(string url, string action){    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);    webRequest.Headers.Add("SOAPAction", action);    webRequest.ContentType = "text/xml;charset="utf-8"";    webRequest.Accept = "text/xml";    webRequest.Method = "POST";    return webRequest;}private static Xmldocument CreateSoapEnvelope(){    Xmldocument soapEnvelopedocument = new Xmldocument();    soapEnvelopedocument.LoadXml(    @"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/""     xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance""     xmlns:xsd=""http://www.w3.org/1999/XMLSchema"">        <SOAP-ENV:Body> <HelloWorld xmlns=""http://tempuri.org/""      SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/"">     <int1 xsi:type=""xsd:integer"">12</int1>     <int2 xsi:type=""xsd:integer"">32</int2> </HelloWorld>        </SOAP-ENV:Body>    </SOAP-ENV:Envelope>");    return soapEnvelopedocument;}private static void InsertSoapEnvelopeIntoWebRequest(Xmldocument soapEnvelopeXml, HttpWebRequest webRequest){    using (Stream stream = webRequest.GetRequestStream())    {        soapEnvelopeXml.Save(stream);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号