【问题标题】:create http method to return soap action result创建http方法返回soap动作结果
【发布时间】:2017-07-19 13:20:21
【问题描述】:

我在服务器上运行了一个 SOAP 服务,我需要在服务器端连接到他并将结果检索到客户端,我知道我可以将 http 方法与控制器和模型相关联,但我不太了解好吧,我如何实现一个获取请求,并在它没有直接与模型关联时给它我想要的名称。

目前我有这个:

   public class DefaultController : ApiController

    {
        [HttpGet]
        public void ProjectName()
        {
            var _url = "http://cmf-sharepoint/ims/Processes/_vti_bin/lists.asmx";
            var _action = "http://schemas.microsoft.com/sharepoint/soap/GetListItems";

            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 soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body>'<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>8e82569a-27b1-47ec-a557-0cc01e250e4f</listName>'<query><Query><Where><Eq><FieldRef Name='Project_x0020_Status'/> <Value Type='Choice'>Active</Value></Eq></Where><OrderBy><FieldRef Name='Title' Ascending='TRUE' /></OrderBy></Query></query></GetListItems></soap:Body></soap:Envelope>");
            return soapEnvelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

我需要在 get 请求中获取 SOAP resonde 有什么帮助吗?目前我无法访问该路线

【问题讨论】:

    标签: c# asp.net rest asp.net-web-api soap


    【解决方案1】:

    以下示例将原始帖子重构为使用 HttpClient 发出 SOAP 请求并读取响应。

    public class DefaultController : ApiController {
        static readonly HttpClient httpClient = new HttpClient();
    
        public DefaultController() {
    
        }
    
        [HttpGet]
        public async Task<IHttpActionResult> ProjectName() {
            var _url = "http://cmf-sharepoint/ims/Processes/_vti_bin/lists.asmx";
            var _action = "http://schemas.microsoft.com/sharepoint/soap/GetListItems";
    
            var soapEnvelopeXml = CreateSoapEnvelope();
            var soapRequest = CreateRequest(_url, _action, soapEnvelopeXml);
            using (var soapResponse = await httpClient.SendAsync(soapRequest)) {
                var soapResult = await soapResponse.Content.ReadAsStringAsync();
                Console.Write(soapResult);
                var response = Request.CreateResponse(soapResponse.StatusCode, soapResponse, "text/xml");
                return ResponseMessage(response);
            }
        }
    
        private static HttpRequestMessage CreateRequest(string url, string action, XmlDocument soapEnvelopeXml) {
            var request = new HttpRequestMessage(method: HttpMethod.Post, requestUri: url);
            request.Headers.Add("SOAPAction", action);
            request.Headers.Add("ContentType", "text/xml;charset=\"utf-8\"");
            request.Headers.Add("Accept", "text/xml");
            request.Content = new StringContent(soapEnvelopeXml.ToString(), Encoding.UTF8, "text/xml"); ;
            return request;
        }
    
        private static XmlDocument CreateSoapEnvelope() {
            XmlDocument soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body>'<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>8e82569a-27b1-47ec-a557-0cc01e250e4f</listName>'<query><Query><Where><Eq><FieldRef Name='Project_x0020_Status'/> <Value Type='Choice'>Active</Value></Eq></Where><OrderBy><FieldRef Name='Title' Ascending='TRUE' /></OrderBy></Query></query></GetListItems></soap:Body></soap:Envelope>");
            return soapEnvelop;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      相关资源
      最近更新 更多