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

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

问题描述

我要连接到第三方Web服务,不提供任何WSDL也不ASMX。该服务的URL就是 HTTP://server/service.soap

我已经关于裸服务调用这篇文章 ,但我不知道这是否是我要找的。

另外,我要求WSDL文件,但被告知,有没有(有没有)。

我使用C#在.NET 2.0,并且无法升级到3.5(所以没有WCF还)。我认为,第三方使用Java,因为这是他们所提供的例子。

在此先感谢!

更新浏览此网址时得到这个答复:

 < SOAP-ENV:信封>
< SOAP-ENV:身体与GT;
< SOAP-ENV:故障>
<故障code取代; SOAP-ENV:服务器16; /故障code取代;
<在faultstring>
找不到在ENVELOPPE body标签
< /在faultstring>
< / SOAP-ENV:故障>
< / SOAP-ENV:身体与GT;
< / SOAP-ENV:信封>
 

解决方案

好吧,我终于得到了这个工作,所以我会写在这里的code我使用。 (请记住,NET 2.0,并没有从WSDL Web服务来获得)。

首先,我们创建一个HttpWebRequest的:

 公共静态HttpWebRequest的CreateWebRequest(字符串URL)
{
    HttpWebRequest的WebRequest的=(HttpWebRequest的)WebRequest.Create(URL);
    webRequest.Headers.Add(SOAP:行动);
    webRequest.ContentType =为text / xml;字符集= \UTF-8 \;
    webRequest.Accept =为text / xml;
    webRequest.Method =POST;
    返回的WebRequest;
}
 

接下来,我们做一个调用Web服务,以及所需的所有值传递。当我从一个XML文档中读取SOAP信封,我会处理的数据作为StringDictionary。应该有更好的方式来做到这一点,但我会觉得这个以后:

 公共静态的XmlDocument ServiceCall(字符串URL,诠释服务,StringDictionary数据)
{
    HttpWebRequest的要求= CreateWebRequest(URL);

    XmlDocument的soapEnvelopeXml = GetSoapXml(服务数据);

    使用(流流= request.GetRequestStream())
    {
        soapEnvelopeXml.Save(流);
    }

    IAsyncResult的asyncResult = request.BeginGetResponse(NULL,NULL);

    asyncResult.AsyncWaitHandle.WaitOne();

    字符串SOA presult;
    使用(WebResponse类WebResponse类= request.EndGetResponse(asyncResult))
    使用(StreamReader的RD =新的StreamReader(webResponse.GetResponseStream()))
    {
        SOA presult = rd.ReadToEnd();
    }

    File.WriteAllText(HttpContext.Current.Server.MapPath(/ SERVICIOS /+ DateTime.Now.Ticks.ToString()+assor_r+ service.ToString()+的.xml),SOA presult) ;

    XmlDocument的RESP =新的XmlDocument();

    resp.LoadXml(SOA presult);

    返回RESP;
}
 

所以,仅此而已。如果有人认为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. Thanks for the help!

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

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