2012-03-15 141 views
1

我正在嘗試執行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

回答

0

用這種方法做出正確的soap請求並不是很容易。我強烈建議使用,而整個Web服務,而作出這樣的要求:

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

這將完成所有上面的代碼;)

相關問題