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
Unable to consume web services using c# but working on postman
Unable to consume web services using c# but working on postman

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 rn 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">rnt<soapenv:Header>rntt<UsernameToken>username</UsernameToken>rntt<PasswordText>password</PasswordText>rntt<SessionType>None</SessionType>rnt</soapenv:Header>rn   <soapenv:Body>rn      <hhm:CreateFollowup_Input>rn         <hhm:FollowupAction>testing</hhm:FollowupAction>rn         <hhm:CloseOutSubReason></hhm:CloseOutSubReason>rn         <hhm:FolloUpStatus>Open</hhm:FolloUpStatus>rn         <hhm:Object_spcId>1-3B39WBFE</hhm:Object_spcId>rn         <hhm:Model></hhm:Model>rn         <hhm:CloseReason></hhm:CloseReason>rn         <hhm:FollowUpDate>08/20/2020</hhm:FollowUpDate>rn         <hhm:ExpectedPurchaseDate></hhm:ExpectedPurchaseDate>rn         <hhm:FollowUpQuestions></hhm:FollowUpQuestions>rn         <hhm:FollowUpDone></hhm:FollowUpDone>rn         <hhm:DSEName>10086S20</hhm:DSEName>rn         <hhm:MakeBought></hhm:MakeBought>rn      </hhm:CreateFollowup_Input>rn   </soapenv:Body>rn</soapenv:Envelope>", ParameterType.RequestBody);
        RestSharp.IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
        return response.Content.ToString();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x