【问题标题】:Web Service always return empty plain/textWeb 服务总是返回空的纯文本/文本
【发布时间】:2012-09-07 00:11:43
【问题描述】:

一直在使用Service Reference,但没有成功:

Web service return only XML

现在我正在使用原始 SOAP 消息来执行此操作:

XmlDocument doc = new XmlDocument();
                doc.Load("Service.xml");

                // create the request to your URL
                Uri wsHost = new Uri("http://www.rrr.net/services/Connect");
                HttpWebRequest request = (HttpWebRequest) WebRequest.Create(wsHost);

                // add the headers
                // the SOAPACtion determines what action the web service should use
                request.Headers.Add("SOAPAction", "act");

                // set the request type
                request.ContentType = "text/xml;charset=\"utf-8\"";
                request.Accept = "text/xml";
                request.Method = "POST";

                // add our body to the request
                Stream stream = request.GetRequestStream();
                doc.Save(stream);
                stream.Close();

                // get the response back
                using( HttpWebResponse response = (HttpWebResponse)request.GetResponse() )
                {
                    Stream dataStream = response.GetResponseStream();
                    StreamReader dataReader = new StreamReader(dataStream); 

                    // Use Linq to read the xml response
                    using (XmlReader reader = XmlReader.Create(dataStream))
                    {

帖子是正确的,但是response总是给我text/plain空结果,响应头:

Headers = {Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain
Date: Thu, 06 Sep 2012 15:59:28 GMT

}

SOAP 消息是,act 是函数:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webService">
  <soapenv:Header/>
  <soapenv:Body>
    <web:act>
      <web:d1>1</web:d1>
      <web:d2>14</web:d2>
    </web:act>
  </soapenv:Body>
</soapenv:Envelope>

我使用SoapUI,下面是来自SoapUI的原始请求,它返回一个xml结果:

POST http://www.rrr.net/services/Connect HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Content-Length: 516
Host: www.rrr.net
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

谢谢。

【问题讨论】:

  • 原始标签的输出是什么?
  • empty content-type text/plain.

标签: c# soap-client


【解决方案1】:

SOAP 网络服务需要指定动作/方法(并且不能为空)。如果您不知道要执行哪个操作,可以通过使用 queryString "?WSDL" 调用 Web 服务来查看 Web 服务 WSDL。 IE。 www.yourSite.com/your/Web/Service/URL?WSDL

【讨论】:

  • SOAPAction 只是函数名?
  • 我放了SOAPAction,还是没有结果。
  • 但是,Web 服务如何知道您要调用的方法中的哪一个(可能是多个)?这就是为什么您必须使用它可以识别的参数调用 Web 服务的原因。 SOAPAction 应该包含您尝试调用的实际 Web 服务方法。
  • 我添加的函数没有结果,问题是SoapUI正在发布SOAPAction:“”,一个空的SOAPACTION,它仍然返回一个结果。
  • 您使用的网址是否正确?你使用正确的方法吗?谁编写了网络服务?您的网络服务是否正确且成功部署?尝试使用“?WSDL”查询字符串访问 Web 服务的实际 URL,你会得到什么?
【解决方案2】:

您必须在请求和服务接口中都指定一个操作。您可以使用下面显示的属性在接口成员上设置操作值,然后使用您使用的方法在请求中设置操作值,但通过指定您在合同中使用的操作名称:

接口成员的属性

[OperationContract Name="YourActionName"]
[WebInvoke (Method = "POST", UriTemplate = "YourActionName")]
Message YourServiceFunction();

一种指定消息操作的方法

Message inputMessage = Message.CreateMessage (MessageVersion.Soap, "YourActionName", reader);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2014-08-10
    • 2014-01-12
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多