Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to soap services, i want to call public soap api in c#. here is my code:-

i want to call this public sop api
Public SOAP APIs[^]

        public static void CallWebService()
        {
            
var _url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso"
            var _action= "";

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);

            using (Stream stm = webRequest.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soapEnvelopeXml);
                }
            }

            // 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  
            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();
            try
            {
                string path =@"C:\myfiles\XMLFile.xml";
                if (File.Exists(path))
                {
                    soapEnvelop.Load(path);
                    string xmlcontents = soapEnvelop.InnerXml;
                    soapEnvelop.LoadXml(xmlcontents);
                }
              
            }
            catch (Exception ex) 
            {

                throw ex;
            }
          
            return soapEnvelop;
        }



i got 500 internal server error. can anyone suggest me the solution

What I have tried:

i take refrence from this solution How to call a soap service in C# - Quora[^]
Posted
Updated 31-Oct-22 0:16am
v2
Comments
Chris Copeland 31-Oct-22 6:13am    
A 500 error code could indicate you're sending across something unsupported by the server. If a server produces a 500 response it could mean they're not setup to handle a certain scenario, so I'd maybe look at the XML content you're sending across and whether you're using the correct URL with the correct headers.

I'd also advise debugging the code (put in a break-point somewhere) and check the content you're sending across. It looks like you're calling both soapEnvelop.load() and soapEnvelop.LoadXml() which is strange.

1 solution

I just left a comment but then immediately noticed this:

C#
using (Stream stm = webRequest.GetRequestStream())
{
    using (StreamWriter stmw = new StreamWriter(stm))
    {
        stmw.Write(soapEnvelopeXml); <--------
    }
}

The TextWriter.Write Method (System.IO) | Microsoft Learn[^] (StreamWriter inherits from TextWriter) calls ToString() on the object being passed in. This is probably not what you want to happen, you probably want the content of the XML file to be sent direct?

Could you not just read the contents of XMLFile.xml into a string and then write that content directly to the StreamWriter?
 
Share this answer
 
Comments
TCS54321 31-Oct-22 6:26am    
Thank you for your solution.
i use this code
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}

and now my code is working.
Richard Deeming 31-Oct-22 7:13am    
That still suggests an error with the service implementation: if the data you send it is invalid, it should be returning a 4xx response instead of a 500 Internal Server Error response.
Chris Copeland 31-Oct-22 9:19am    
Yeah I dropped a similar comment on the question. I'm not surprised at all that some servers don't handle things like that, I can't count the number of production APIs I've had to integrate with that churn out 500s like no tomorrow..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900