繁体   English   中英

无法使用 c# 使用 Web 服务,但正在使用邮递员

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

我正在尝试通过 httpwebrequest 调用 webservice。 已成功添加 XML 文档和标题,但仍然收到内部服务器错误 500 错误 从调试模式复制的相同 xml 文档在邮递员上工作正常。 我的代码是

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

我还尝试绕过 ssl 证书,因为 webservice 服务器 ssl 证书已过期或不受信任。 请帮忙,因为我被卡住了。 我也尝试过 WSDL 文件,但也无法成功。

编辑在这里我添加图像我如何在邮递员中尝试网络服务在此处输入图片说明 在此处输入图片说明

解决了

经过艰苦的寻找,不知何故,我知道如果 webservice 在邮递员上成功运行,它还会提供该语言的代码(就在保存按钮下方)。 所以我使用了该代码(基于RestClient )。 我的错误是没有在 xml doc 中使用 \\r\\n 和适当的空格 所以 xml 是问题所在。

//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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM