执行SOAP请求时出现内部服务器错误(500)

时间:2012-03-15 11:11:12

标签: c# xml web-services soap httpwebrequest

我正在尝试执行SOAP请求,但服务器返回500错误。 例如,SOAP请求通过jmeter正确地返回XML消息,所以它必须是我的代码中的东西,但我看不清楚是什么。你能帮忙吗?

private void soapRequest(string regID)
        {

            string soapReq= @"<?xml version=""1.0"" encoding=""utf-8""?>";
            soapReq= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:mpc=\"urn://mobility-platform.de/mpcustomerdb/\">\n";
            soapReq += "<soapenv:Header/>\n";
            soapReq += "<soapenv:Body>\n";
            soapReq += "<mpc:findRegistrationByID>\n";
            soapReq += "<registrationID>" + regID + "</registrationID>\n";
            soapReq += "</mpc:findRegistrationByID>\n";
            soapReq += "</soapenv:Body>\n";
            soapReq += "</soapenv:Envelope>";

            //Builds the connection to the WebService.
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://url?wsdl");
            req.Credentials = new NetworkCredential("user", "pass");            
            req.Headers.Add("SOAP:Action");
            req.ContentType = "text/xml;charset=\"utf-8\"";
            req.Accept = "text/xml";
            req.Method = "POST";

            //Passes the SoapRequest String to the WebService
            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soapReq.ToString());                    
                }
            }
            try
            {
                //Gets the response
                HttpWebResponse soapResponse = (HttpWebResponse)req.GetResponse();
                //Writes the Response
                Stream responseStream = soapResponse.GetResponseStream();
                //read the stream
                XmlDocument soapResponseXML = new XmlDocument();

                StreamReader responseStreamRead = new StreamReader(responseStream);
                soapResponse.ContentType = "text/xml";
                //MessageBox.Show(responseStreamRead.ReadToEnd().ToString());
                string soapURL = responseStreamRead.ReadToEnd().ToString();

                soapResponseXML.LoadXml(soapURL);
            }
            catch (Exception ex)
            {                
                MessageBox.Show("Error: " + ex.Message);
            }
        }

这是肥皂要求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mpc="urn://mobility-platform.de/mpcustomerdb/">
   <soapenv:Header/>
   <soapenv:Body>
      <mpc:findRegistrationByID>
         <registrationID>2304580</registrationID>
      </mpc:findRegistrationByID>
   </soapenv:Body>
</soapenv:Envelope>

稍后编辑: 如果我改变了
req.Headers.Add("SOAP:Action");
req.Headers.Add("SOAPAction", ""\"http://url\"" + "findRegistrationByID"); 我得到一个不同的错误:  “此类属性未由此类实现”

1 个答案:

答案 0 :(得分:0)

使用此方法制作正确的肥皂请求并不容易。我强烈建议使用整个Web服务,而不是像这样提出请求:

WebService aa = new WebService;
registrationName = aa.findRegistrationByID("123");

这将完成以上所有代码;)