Web Service始终返回空的plain / text

时间:2012-09-06 15:32:47

标签: c# soap-client

一直在使用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)

谢谢。

2 个答案:

答案 0 :(得分:0)

SOAP webservices需要指定操作/方法(而不是空)。如果您不知道您想要哪个操作,可以通过使用queryString“?WSDL”调用webservice来查看webservice WSDL。即www.yourSite.com/your/Web/Service/URL?WSDL

答案 1 :(得分:0)

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

接口成员上的属性

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

指定消息操作的一种方法

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