Search code examples
c#asp.netweb-servicespostmanwebrequest

Unable to consume web services using c# but working on postman


I am trying to call webservice through httpwebrequest. Successfully added XML document and headers but still getting error Internal Server Error 500 . Same xml doc copied from debugging mode is working fine on postman. My code is

public HttpWebRequest CreateWebRequest(string url, string action)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction",action);
        //webRequest.Headers.Add("ContentType","text/xml;charset=\"utf-8\"");
         webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        //  "charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }
public string CallWebService()
    {
        try
        {
           
            var _url = "https://xxxxxxxxxx/siebel/app/eai/enu?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1";
            var _action = @"""document/http://yyyyyy/:CreateFollowup""";

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            
            ServicePointManager
                .ServerCertificateValidationCallback +=
            (sender, cert, chain, sslPolicyErrors) => true;
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            //InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
            
                Invoke(new Action(() =>
                {
                    listBox_Log.Items.Add("saving soapenvelope.");
                }));
                soapEnvelopeXml.Save(webRequest.GetRequestStream());
            
            // 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 usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();
            
            // get the response from the completed web request.
            string soapResult = null;

            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                Invoke(new Action(() =>
                {
                    listBox_Log.Items.Add("fetching response");
                }));
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    Invoke(new Action(() =>
                    {
                        listBox_Log.Items.Add("collecting response");
                    }));
                    soapResult = rd.ReadToEnd();
                }
                Console.Write(soapResult);
            }
            return soapResult;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
public XmlDocument CreateSoapEnvelope()
    {
       
        XmlDocument soapEnvelop = new XmlDocument();
        
        soapEnvelop.LoadXml(@"xml code");
        return soapEnvelop;
    }

I have also tried bypassing ssl certificate because webservice server ssl certificate expired or not trusted. Please help as i am stuck. I have also tried WSDL file but couln't succeed for that as well.

Edit Here i am adding images how i have tried webservice in postman enter image description here enter image description here


Solution

  • Solved

    After very hard finding, somehow i got to know that if webservice run successfully on postman that it also provide code for that language (just below save button). So i used that code (Based on RestClient). My mistake was not using \r\n and proper spaces in xml doc. So xml was the issue.

    //THis code used for ssl bypassing
    ServicePointManager
                    .ServerCertificateValidationCallback +=
                (sender, cert, chain, sslPolicyErrors) => true;
    
    
            var client = new RestSharp.RestClient(webservice_url)
            {
                Timeout = -1
            };
            var request = new RestSharp.RestRequest(Method.POST);
            request.AddHeader("Content-Type", "text/xml");
            request.AddHeader("SOAPAction", "action");
            request.AddParameter("text/xml", "<soapenv:Envelope xmlns:soapenv=\"url1" xmlns:hhm=\"url2">\r\n\t<soapenv:Header>\r\n\t\t<UsernameToken>username</UsernameToken>\r\n\t\t<PasswordText>password</PasswordText>\r\n\t\t<SessionType>None</SessionType>\r\n\t</soapenv:Header>\r\n   <soapenv:Body>\r\n      <hhm:CreateFollowup_Input>\r\n         <hhm:FollowupAction>testing</hhm:FollowupAction>\r\n         <hhm:CloseOutSubReason></hhm:CloseOutSubReason>\r\n         <hhm:FolloUpStatus>Open</hhm:FolloUpStatus>\r\n         <hhm:Object_spcId>1-3B39WBFE</hhm:Object_spcId>\r\n         <hhm:Model></hhm:Model>\r\n         <hhm:CloseReason></hhm:CloseReason>\r\n         <hhm:FollowUpDate>08/20/2020</hhm:FollowUpDate>\r\n         <hhm:ExpectedPurchaseDate></hhm:ExpectedPurchaseDate>\r\n         <hhm:FollowUpQuestions></hhm:FollowUpQuestions>\r\n         <hhm:FollowUpDone></hhm:FollowUpDone>\r\n         <hhm:DSEName>10086S20</hhm:DSEName>\r\n         <hhm:MakeBought></hhm:MakeBought>\r\n      </hhm:CreateFollowup_Input>\r\n   </soapenv:Body>\r\n</soapenv:Envelope>", ParameterType.RequestBody);
            RestSharp.IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            return response.Content.ToString();