来自文档C#的SOAP请求

时间:2016-08-09 19:20:29

标签: c# asp.net soap

我有一个.aspx文件,我正在尝试调用API服务(在我们的服务器内部托管)。

我正在尝试在同一个文件中执行此操作(不在幕后) - 这可能吗?

这是我的.aspx文件:

<%@ Language="C#" debug="true"%>
<HTML>
    <script runat="server" language="C#">
        public void Page_Load(object sender, EventArgs e)
        {
            //setup Request
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest) 
            System.Net.HttpWebRequest.Create("https://my.fully.qualified.server/EMSAPI/");  
            //format our SOAP request
            string strRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
                            "<soap12:Envelope" +
                            " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                            " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
                            " xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
                            "<soap12:Body>" +
                                    "<GetAllBookings xmlns=\"http://DEA.API.WEB.SERVICE/\">" +
                                            "<UserName>username</UserName>" +
                                            "<Password>password</Password>" +
                                            "<StartDate>"+
                                            "2016-08-08T00:00:00.000"+
                                            "</StartDate>" +
                                            "<EndDate>"+
                                            "2016-08-09T00:00:00.000"+
                                            "</EndDate>" +
                                            "<BuildingID>36</BuildingID>" +
                                            "<ViewComboRoomComponents>false</ViewComboRoomComponents>" +
                                        "</GetAllBookings>" +
                            "</soap12:Body>" +
                            "</soap12:Envelope>";
            //String xmlString = strRequest; 
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
            byte[] bytesToWrite = encoding.GetBytes(strRequest); 
            request.Method = "POST"; 
            request.ContentLength = bytesToWrite.Length; 
            request.Headers.Add("SOAPAction: \"https://my.fully.qualified.server/EMSAPI/\"");
            request.ContentType = "application/soap+xml; charset=utf-8"; 
            System.IO.Stream newStream = request.GetRequestStream(); 
            newStream.Write(bytesToWrite, 0, bytesToWrite.Length); 
            newStream.Close(); 
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); 
            System.IO.Stream dataStream = response.GetResponseStream(); 
            System.IO.StreamReader reader = new System.IO.StreamReader(dataStream); 
            string responseFromServer = reader.ReadToEnd();
        }
    </script>
    <body>

    </body>
</HTML>

我收到以下错误:

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error:


Line 58:     newStream.Close(); 
Line 59: 
(THIS IS BOLDED) Line 60:     System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); 
Line 61:     System.IO.Stream dataStream = response.GetResponseStream(); 
Line 62:     System.IO.StreamReader reader = new System.IO.StreamReader(dataStream); 

对可能出现的问题或如何进一步排除故障的想法?

1 个答案:

答案 0 :(得分:0)

我最终与我们的系统管理员交谈并确保我们的网络目录已配置为应用程序。

然后我创建了一个新的Web应用程序,并添加了一个指向API的服务引用。

  • 右键点击参考文献
  • 点击“添加服务参考”&#39;
  • 加入你的 API URL(到您的.wsdl位置)
  • 命名您的服务参考
  • 访问 它以编程方式(即: theService.SoapClient client = new theService.SoapClient();

然后在服务参考的References.cs文档中执行API为您提供的方法所需的操作!