【问题标题】:How to call a web service with no wsdl in .net如何在 .net 中调用没有 wsdl 的 Web 服务
【发布时间】:2010-11-19 14:49:49
【问题描述】:

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

我已阅读 this article 关于原始服务调用的信息,但我不确定这是否是我正在寻找的。​​p>

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

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

提前致谢!

UPDATE 浏览网址时收到此响应:

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

【问题讨论】:

  • 您应该在某处发布一个或多个 Java 示例文件,然后在此处发布指向它们的链接。一种可能性是snipt.org
  • 我已经成功连接了网络服务,一旦我有时间我会发布如何做的。感谢您的关注!

标签: c# .net web-services soap wsdl


【解决方案1】:

好吧,我终于让它工作了,所以我将在这里编写我正在使用的代码。 (记住,.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";
    return webRequest;
}

接下来,我们调用 web 服务,传递所有需要的值。当我从 xml 文档中读取肥皂信封时,我会将数据作为 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 添加到答案中,我会写下来。

【讨论】:

  • 恕我直言,我认为必须将GetSoapXml添加到答案中,请你写下来
  • 如您所见,这是一个已有 8 年历史的问题,因此现在编写 GetSoapXml 有点复杂,因为我不再在同一家公司工作,所以我无法访问代码。
【解决方案2】:

在我看来,SOAP Web 服务没有理由不提供 WSDL。它不需要由服务动态生成;它不需要在 Internet 上可用。但是必须有 WSDL,即使他们必须通过 软盘拇指驱动器将其发送给您!

如果您有任何能力向该服务的提供者投诉,那么我敦促您这样做。如果你有能力反击,那就这样做。理想情况下,切换服务提供商,并告诉这些人这是因为他们没有提供 WSDL。至少,找出他们认为这不重要的原因。

【讨论】:

  • 记录了发送和检索信息的 XML 方案。我唯一缺乏的是正确连接它的知识。至于提供它的公司,是一家保险公司,所以没有可能投诉:(
  • 实际上,我非常不同意这一点。这些模式是来自行业标准组织(国际与否)的任何行业标准的一部分。如果是这样,这会给你一个单一的推动点,以及一个充满其他开发者的行业,他们可以和你一起推动。这不像您要求他们做一些非常复杂或昂贵的事情,特别是如果他们已经有了分发模式的机制。即使是公司特有的,一旦他们获得了架构,剩下的就很容易了。
  • 运气好能找出他们不想提供 WSDL 的原因吗?也许您应该试用一下 XMLspy,并向他们展示它是多么容易,因为他们已经获得了 XML 模式。
  • 不回答问题。有时你必须用你得到的东西工作。 -1
  • @guy 和自满是十年后你仍然会遇到同样问题的原因。
【解决方案3】:

我已经创建了以下帮助方法来手动调用 WebService 没有任何参考:

public static HttpStatusCode CallWebService(string webWebServiceUrl, 
                                            string webServiceNamespace, 
                                            string methodName, 
                                            Dictionary<string, string> parameters, 
                                            out string responseText)
{
    const string soapTemplate = 
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
   xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
   xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
  <soap:Body>
    <{0} xmlns=""{1}"">
      {2}    </{0}>
  </soap:Body>
</soap:Envelope>";

    var req = (HttpWebRequest)WebRequest.Create(webWebServiceUrl);
    req.ContentType = "application/soap+xml;";
    req.Method = "POST";

    string parametersText;

    if (parameters != null && parameters.Count > 0)
    {
        var sb = new StringBuilder();
        foreach (var oneParameter in parameters)
            sb.AppendFormat("  <{0}>{1}</{0}>\r\n", oneParameter.Key, oneParameter.Value);

        parametersText = sb.ToString();
    }
    else
    {
        parametersText = "";
    }

    string soapText = string.Format(soapTemplate, methodName, webServiceNamespace, parametersText);


    using (Stream stm = req.GetRequestStream())
    {
        using (var stmw = new StreamWriter(stm))
        {
            stmw.Write(soapText);
        }
    }

    var responseHttpStatusCode = HttpStatusCode.Unused;
    responseText = null;

    using (var response = (HttpWebResponse)req.GetResponse())
    {
        responseHttpStatusCode = response.StatusCode;

        if (responseHttpStatusCode == HttpStatusCode.OK)
        {
            int contentLength = (int)response.ContentLength;

            if (contentLength > 0)
            {
                int readBytes = 0;
                int bytesToRead = contentLength;
                byte[] resultBytes = new byte[contentLength];

                using (var responseStream = response.GetResponseStream())
                {
                    while (bytesToRead > 0)
                    {
                        // Read may return anything from 0 to 10. 
                        int actualBytesRead = responseStream.Read(resultBytes, readBytes, bytesToRead);

                        // The end of the file is reached. 
                        if (actualBytesRead == 0)
                            break;

                        readBytes += actualBytesRead;
                        bytesToRead -= actualBytesRead;
                    }

                    responseText = Encoding.UTF8.GetString(resultBytes);
                    //responseText = Encoding.ASCII.GetString(resultBytes);
                }
            }
        }
    }

    // standard responseText: 
    //<?xml version="1.0" encoding="utf-8"?>
    //<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    //    <soap:Body>
    //        <SayHelloResponse xmlns="http://tempuri.org/">
    //            <SayHelloResult>Hello</SayHelloResult>
    //        </SayHellorResponse>
    //    </soap:Body>
    //</soap:Envelope>
    if (!string.IsNullOrEmpty(responseText))
    {
        string responseElement = methodName + "Result>";
        int pos1 = responseText.IndexOf(responseElement);

        if (pos1 >= 0)
        {
            pos1 += responseElement.Length;
            int pos2 = responseText.IndexOf("</", pos1);

            if (pos2 > pos1)
                responseText = responseText.Substring(pos1, pos2 - pos1);
        }
        else
        {
            responseText = ""; // No result
        }
    }

    return responseHttpStatusCode;
}

然后您可以使用以下代码简单地调用任何 Web 服务方法:

var parameters = new Dictionary<string, string>();
parameters.Add("name", "My Name Here");

string responseText;
var responseStatusCode = CallWebService("http://localhost/TestWebService.asmx", 
                                        "http://tempuri.org/", 
                                        "SayHello", 
                                        parameters, 
                                        out responseText);

【讨论】:

  • 我要试试这个 - 如果它有效,那么为什么你只有一个赞成票将是一个谜。
  • 我对上述内容的唯一必要扩展是包含一个 SoapHeader..
【解决方案4】:

嗯,这里有点棘手,但并非不可能,但我会尽力解释它。

你需要做的是

  1. 创建与您在第三方服务上处理的对象架构相匹配的可序列化类。
  2. 了解他们是否在服务调用中使用了任何 SOAPAction
  3. 看看您是否可以创建一个 asmx,在处理请求和响应方面模仿他们的服务(如果他们的服务关闭,这将有利于测试您的客户端应用程序)
  4. 然后,您可以从虚拟服务创建服务代理,并在调用第三方服务时更改服务 url。
  5. 如果您的客户端出现问题,您可以调整虚拟服务,重新生成代理,然后重试。

当我想到它时,我会尝试添加更多内容,但这应该足以让你开始。

【讨论】:

  • 我会寻找有关此的信息,但请发布您认为有用的任何信息。
  • 是的,希望他们甚至为此提供 XML 模式。 OTOH,如果您要使用虚拟服务执行任何操作,请不要使用 ASMX 执行任何操作 - 改用 WCF。
  • WCF 是否与 2.0 兼容?我认为它只能与 3.0 一起使用
  • 抱歉,忘记了你被困在了古老的过去。您必须使用 ASMX,即使 Microsoft 不再修复其中的错误。
【解决方案5】:

如果幸运的话,您仍然可以获得 wsdl。一些 Web 服务框架允许您检索动态生成的 WSDL。

使用 Axis1.x 编写的 Web 服务允许您通过浏览到 URL 来检索动态生成的 WSDL 文件。

只需浏览到

http://server/service.soap/?wsdl

我不知道其他框架是否可以做到这一点。

【讨论】:

  • 认为这有点远。您知道编写服务的框架(如果有的话)吗?如果没有 WSDL 那么你应该如何使用这个服务呢?他们对你有什么指导方针吗?
  • 他们提交了一些 .java 文件作为示例,但我对 Java 一无所知...
  • 同样的问题我收到这条消息:无法生成 WSDL!此位置没有 SOAP 服务。
  • 今天是今天!谢谢先生! ^.^ 在我的例子中,URL 是:thenameoftheserver/wcf/Service.svc/soap?wsdl
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2019-11-11
相关资源
最近更新 更多