创建http方法以返回soap操作结果

时间:2017-07-19 13:20:22

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

我在服务器上运行了一个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任何帮助吗?目前我无法访问路线

1 个答案:

答案 0 :(得分:0)

以下示例重构原始帖子以使用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;
    }

}