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

问题描述:

我正在尝试执行SOAP请求,但服务器正在返回500错误。 SOAP请求例如通过Jmeter正确地返回XML消息,所以它必须是我的代码中的某些东西,但是我看不到什么。你能帮我吗?执行SOAP请求时发生内部服务器错误(500)

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); 
      } 
     } 

这里是SOAP请求

<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

**一)**切勿从字符串构建XML 。 ** b)**您的'req.Accept =“text/xml”;'可能是错误的。什么'Content-Type'服务器返回成功的响应?这是你应该设置'接受'的人。 – Tomalak 2012-03-15 11:18:18

+1

你有权访问有问题的服务器吗?如果是这样,应用程序日志中的事件查看器应该有一些有用的信息。 – 2012-03-15 13:04:46

+0

我想我在这里做错了:'req.Headers.Add(“SOAP:Action”);'它应该是这样的:'req.Headers.Add(“SOAPAction”,“”\“http://url \“”+“findRegistrationByID”);'但是如果我改变它就像我得到以下错误**此属性不是由这个类实现** – observ 2012-03-15 14:46:24

用这种方法做出正确的soap请求并不是很容易。我强烈建议使用,而整个Web服务,而作出这样的要求:

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

这将完成所有上面的代码;)