C# SOAP请求报错:无法识别服务端点EPR,求排查方案
问题描述
我在使用SOAP请求获取数据时遇到问题,始终收到错误提示:
"The system is not able to identify the service endpoint for service EPR '172.16.25.152/ws/zus.channel.gabinetoweV2:zla'"
以下是我的代码:
public void InvokeService() { try { //Calling CreateSOAPWebRequest method System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; }; string action = @"pobierzOswiadczenie "; XmlDocument soapEnvelopeXml = CreateSoapEnvelope(); HttpWebRequest webRequest = CreateSOAPWebRequest(action); InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); //Geting response from request using (WebResponse webResponse = webRequest.GetResponse()) { using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { //reading stream var ServiceResult = rd.ReadToEnd(); //writting stream result on console Console.WriteLine(ServiceResult); Console.ReadLine(); } } } catch (WebException webex) { WebResponse errResp = webex.Response; using (Stream respStream = errResp.GetResponseStream()) { StreamReader reader = new StreamReader(respStream); string text = reader.ReadToEnd(); } } } public HttpWebRequest CreateSOAPWebRequest(string action) { //Making Web Request string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("ezla_ag:ezla_ag")); HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"https://193.105.143.152:8001/ws/zus.channel.gabinetoweV2:zla"); //SOAPAction Req.Headers.Add("SOAPAction", action); //Content_type Req.ContentType = "application/soap+xml;charset=\"utf-8\""; Req.Accept = "text/xml"; Req.Headers["Authorization"] = "Basic " + encoded; //HTTP method Req.Method = "POST"; //return HttpWebRequest return Req; } private static XmlDocument CreateSoapEnvelope() { XmlDocument SOAPReqBody = new XmlDocument(); SOAPReqBody.LoadXml(@"<?xml version=\"1.0\" encoding=\"utf-8\"?> <soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:zus=\"https://193.105.143.152:8001/ws/zus.channel.gabinetoweV2:zla\"> <soap:Header/> <soap:Body> <zus:pobierzOswiadczenie/> </soap:Body> </soap:Envelope>"); return SOAPReqBody; } private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } }
问题排查与解决建议
从错误信息和代码来看,核心问题是服务端无法识别你请求的端点,下面是几个最可能的原因和对应的解决方向:
端点IP不匹配:你代码里请求的是
193.105.143.152,但错误里的EPR显示的是172.16.25.152。这两个IP不一致,大概率是服务端的配置问题——可能服务内部绑定的是内网IP172.16.25.152,但你访问的是外网IP193.105.143.152,导致服务端无法映射到正确的端点。建议联系服务提供者确认:- 服务对外暴露的正确端点URL是什么?
- 是否存在反向代理或端口映射的配置问题?
SOAPAction格式错误:你设置的SOAPAction是
pobierzOswiadczenie(注意末尾还有空格),这可能不符合服务端的要求。通常SOAPAction需要包含完整的命名空间前缀,比如应该是https://193.105.143.152:8001/ws/zus.channel.gabinetoweV2:zla/pobierzOswiadczenie或者服务WSDL中定义的准确值。建议:- 去掉action末尾的空格;
- 核对服务的WSDL文档(通常可以通过在端点URL后加
?wsdl访问),找到pobierzOswiadczenie操作对应的SOAPAction值,替换进去。
命名空间与服务定义不匹配:你在SOAP信封里定义的zus命名空间是
https://193.105.143.152:8001/ws/zus.channel.gabinetoweV2:zla,但如果服务端的WSDL里的targetNamespace不是这个值,就会导致服务无法识别请求的操作。建议下载服务的WSDL文件,确认其中的targetNamespace,然后修改代码里的命名空间。Content-Type和Accept头不兼容:你设置的
ContentType是application/soap+xml;charset="utf-8"(SOAP 1.2的格式),但Accept是text/xml(SOAP 1.1的格式),这两个头版本不匹配可能导致服务端解析错误。建议统一这两个头:- 如果服务是SOAP 1.1,把ContentType改成
text/xml;charset=utf-8; - 如果是SOAP 1.2,把Accept改成
application/soap+xml。
- 如果服务是SOAP 1.1,把ContentType改成
证书验证的潜在影响:虽然你跳过了证书验证,但如果服务端的证书绑定的是
172.16.25.152,而你访问的是193.105.143.152,可能会导致服务端在处理请求时出现路由混淆。建议确认服务端证书的主体名称是否包含你访问的IP。
内容的提问来源于stack exchange,提问作者Aleksandra

