无法使用c#使用web服务,但正在使用postman

无法使用c#使用web服务,但正在使用postman,c#,asp.net,web-services,postman,webrequest,C#,Asp.net,Web Services,Postman,Webrequest,我正试图通过httpwebrequest调用webservice。已成功添加XML文档和标题,但仍出现错误内部服务器错误500。从调试模式复制的相同xml文档在postman上运行良好。我的代码是 public HttpWebRequest CreateWebRequest(string url, string action) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

我正试图通过httpwebrequest调用webservice。已成功添加XML文档和标题,但仍出现错误内部服务器错误500。从调试模式复制的相同xml文档在postman上运行良好。我的代码是

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文件,但也没有成功

编辑 在这里,我添加了我如何在postman中尝试webservice的图片

已解决

经过非常艰难的寻找,不知何故我知道,如果webservice在postman上成功运行,它也会为该语言提供代码(就在save按钮下方)。因此我使用了该代码(基于RestClient)。 我的错误是没有在xml文档中使用\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();
//此代码用于ssl旁路
服务点管理器
.ServerCertificateValidationCallback+=
(发件人、证书、链、sslPolicyErrors)=>真;
var client=new RestSharp.RestClient(webservice\u url)
{
超时=-1
};
var请求=新的RestSharp.RestRequest(Method.POST);
AddHeader(“内容类型”、“文本/xml”);
请求.AddHeader(“SOAPAction”、“action”);
request.AddParameter(“text/xml”,“\r\n\t\r\n\t\t\t用户名\r\n\t\t密码\r\n\t\t\t\r\n\r\n\r\n\r\n测试\r\n\r\n\r\n打开\r\n 1-3B39WBFE\r\n\r\n\r\n\r\n\r\n 2020年8月20日\r\n\r\n\r\n\n\r\n\n\r\n\n\r\n\n\r\n\n\r\n\n\r\n\n\r\n\r\n,ParameterType.RequestBody);
RestSharp.IRestResponse response=client.Execute(请求);
Console.WriteLine(response.Content);
返回response.Content.ToString();

如果您展示了如何使用Postman创建请求,那么我们将更容易进行比较和对比。您所说的“尝试过的WSDL文件”到底是什么意思,这个描述太模糊了,任何人都不知道你是否正确。这大概是我回答这个问题的1000次了。c#中的默认头与http的其他工具不同。所以解决这个问题的最好方法是使用像wireshark或fiddler这样的嗅探器,比较Postman和c#中的第一个请求。还要检查正在使用的TLS的500错误版本。五年前,由于安全漏洞,业界决定取消TLS 1.0/1.1。微软在6月份推出了一项安全更新,在服务器上禁用TLS 1.0/1.1。客户必须确保正在使用TLS 1.2或1.3。未指定时,将使用默认版本的操作系统。@ADyson我已为您添加了邮递员图像。这里使用的是相同的头和xml体。对于WSDL文件,我使用AddServiceReference->AddWebReference添加了它。然后使用它的功能。但获取错误客户端发现响应内容类型为“”,但应为“text/xml”。请求失败,错误为空response@jdweng请你能说得更具体或更清楚些,因为我是网络服务的初学者。这是我第一次。早些时候,我收到一个错误,您提到基础连接已关闭:无法为SSL/TLS安全通道建立信任关系。-->System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效。。但后来我添加了来自internet的证书旁路代码,它显示了新的错误InternalServerError500。也许你说的是对的,但我没听懂。