我必须连接到不提供 wsdl 或 asmx 的第三方 Web 服务。该服务的 url 只是 http://server/service.soap
我已阅读 this article关于原始服务调用,但我不确定这是否是我要找的。p>
此外,我要求提供 wsdl 文件,但被告知没有(而且不会有)。
我在 .net 2.0 中使用 C#,无法升级到 3.5(因此还没有 WCF)。我认为第三方正在使用 java,因为这是他们提供的示例。
提前致谢!
更新 浏览 url 时获取此响应:
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>
Cannot find a Body tag in the enveloppe
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
最佳答案
好吧,我终于让它工作了,所以我会在这里写下我正在使用的代码。 (请记住,.Net 2.0,并且没有可从 Web 服务获取的 wsdl)。
首先,我们创建一个 HttpWebRequest:
public static HttpWebRequest CreateWebRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
接下来,我们调用网络服务,传递所有需要的值。当我从 xml 文档中读取 soap 信封时,我会将数据作为 StringDictionary 进行处理。应该是更好的方法,但我稍后会考虑:
public static XmlDocument ServiceCall(string url, int service, StringDictionary data)
{
HttpWebRequest request = CreateWebRequest(url);
XmlDocument soapEnvelopeXml = GetSoapXml(service, data);
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
IAsyncResult asyncResult = request.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
string soapResult;
using (WebResponse webResponse = request.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
File.WriteAllText(HttpContext.Current.Server.MapPath("/servicios/" + DateTime.Now.Ticks.ToString() + "assor_r" + service.ToString() + ".xml"), soapResult);
XmlDocument resp = new XmlDocument();
resp.LoadXml(soapResult);
return resp;
}
就这样吧。如果有人认为必须将 GetSoapXml 添加到答案中,我会写下来。
关于c# - 如何在 .net 中调用没有 wsdl 的 web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1277212/