【问题标题】:Consume web service dynamically using HttpWebRequest without service reference使用 HttpWebRequest 动态使用 Web 服务,无需服务引用
【发布时间】:2016-11-03 05:14:16
【问题描述】:

我工作的应用程序必须将数据发送到外部系统。系统系统将有一个 Web 服务(c# 或 java 或 php),我需要使用它。由于外部系统与客户端一样多,因此我需要获取 WSDL 文件、方法名称和参数作为用户输入并将数据发送到外部系统。

所以,我正在尝试使用可用的代码动态调用 Web 服务 here

我测试了几个可用的免费网络服务here

我从 WSDL 文件中找到 SOAP 位置、方法名称和参数,并提供与输入相同的内容。

以下服务按预期工作

http://soaptest.parasoft.com/calculator.wsdl
Location - http://ws1.parasoft.com/glue/calculator
Method Name - add
Parameter - x,y

但是,当我通过提供 SOAP 位置和方法名称为另一个免费服务尝试相同操作时,它会引发 500 Internal server error。

http://www.predic8.com:8080/crm/CustomerService?wsdl
Location - http://www.predic8.com:8080/crm/CustomerService
Method Name - getAll

我通过在soapUI 中测试上述wsdl 确认这些输入是正确的。在soapUI请求窗口中使用相同的位置。

我不确定它为什么会引发错误。请帮我理解一下。

另外请告诉我是否可以从 WSDL 文件获取服务位置并使用 HttpWebRequest 获取响应。恐怕无论用于实现 Web 服务的技术如何,这种调用 Web 服务的方法都有效。

编辑:

问题似乎与 SOAP 信封有关。

对于http://soaptest.parasoft.com/calculator.wsdl,即使我们忽略xmlns:cal="http://www.parasoft.com/wsdl/calculator/,它也会成功执行。

但是对于 http://www.predic8.com:8080/crm/CustomerService?wsdl ,如果我忽略 xmlns:ns="http://predic8.com/wsdl/crm/CRMService/1/,它会引发内部服务器错误。

请在此处分享我如何成为普通人

【问题讨论】:

  • 能否请您出示导致错误的请求?
  • 技术混蛋,您有机会看看答案是否有帮助?
  • 时间结束了,但也许这个链接可以帮助某人:diogonunes.com/blog/…

标签: c# web-services soap wsdl httpwebrequest


【解决方案1】:

问题似乎与您正在使用的请求数据有关。请求验证失败,错误也是。

如果你导入提供的wsdl,并验证创建请求,问题就很清楚了。

PersonCustomer 下有一个元素id,数据必须遵循在架构../common/1 下定义的特定模式

这里是架构引用,它有simpleType restriction

   <xsd:simpleType name="IdentifierType">
      <xsd:annotation>
         <xsd:documentation>Identifier for business objects.</xsd:documentation>
      </xsd:annotation>
      <xsd:restriction base="xsd:string">
         <xsd:pattern value="[A-Z]{2}-\d{5}"/>
      </xsd:restriction>
   </xsd:simpleType>

所以,id 的值应该类似于 AA-12345,即两个大写字母、连字符 (-),后跟 5 位数字。

按照上述模式更改您的请求,您应该会很好。

希望这有帮助。

【讨论】:

    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    相关资源
    最近更新 更多