如何在 .net 中调用没有 wsdl 的 Web 服务 [英] How to call a web service with no wsdl in .net

查看:25
本文介绍了如何在 .net 中调用没有 wsdl 的 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须连接到不提供 wsdl 或 asmx 的第三方 Web 服务.服务的 url 只是 http://server/service.soap

我已阅读这篇文章 关于原始服务调用,但我不确定这是否是我要找的.

另外,我要求提供 wsdl 文件,但被告知没有(而且不会有).

我在 .net 2.0 中使用 C#,但无法升级到 3.5(所以还没有 WCF).我认为第三方正在使用 java,因为这是他们提供的示例.

提前致谢!

UPDATE 浏览 url 时获取此响应:

<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><故障字符串>在信封中找不到 Body 标签</故障字符串></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

解决方案

好吧,我终于让它工作了,所以我会在这里写我正在使用的代码.(请记住,.Net 2.0,并且没有 wsdl 可以从 Web 服务中获取).

首先,我们创建一个 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";返回 webRequest;}

接下来,我们调用 web 服务,传递所有需要的值.当我从 xml 文档中读取 soap 信封时,我会将数据作为 StringDictionary 处理.应该是一个更好的方法,但我稍后会考虑:

public static XmlDocument ServiceCall(string url, int service, StringDictionary data){HttpWebRequest 请求 = CreateWebRequest(url);XmlDocument soapEnvelopeXml = GetSoapXml(service, data);使用(流流 = request.GetRequestStream()){soapEnvelopeXml.Save(stream);}IAsyncResult asyncResult = request.BeginGetResponse(null, null);asyncResult.AsyncWaitHandle.WaitOne();字符串soapResult;使用 (WebResponse webResponse = request.EndGetResponse(asyncResult))使用 (StreamReader rd = new StreamReader(webResponse.GetResponseStream())){肥皂结果 = 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);返回响应;}

所以,仅此而已.如果有人认为必须将 GetSoapXml 添加到答案中,我会写下来.

I have to connect to a third party web service that provides no wsdl nor asmx. The url of the service is just http://server/service.soap

I have read this article about raw services calls, but I'm not sure if this is what I'm looking for.

Also, I've asked for wsdl files, but being told that there are none (and there won't be).

I'm using C# with .net 2.0, and can't upgrade to 3.5 (so no WCF yet). I think that third party is using java, as that's the example they have supplied.

Thanks in advance!

UPDATE Get this response when browsing the 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>

解决方案

Well, I finally got this to work, so I'll write here the code I'm using. (Remember, .Net 2.0, and no wsdl to get from web service).

First, we create an 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;
}

Next, we make a call to the webservice, passing along all values needed. As I'm reading the soap envelope from a xml document, I'll handle the data as a StringDictionary. Should be a better way to do this, but I'll think about this later:

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

So, that's all. If anybody thinks that GetSoapXml must be added to the answer, I'll write it down.

这篇关于如何在 .net 中调用没有 wsdl 的 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆