使用TcpClient调用TCP SoapService

时间:2009-01-26 09:02:13

标签: c# web-services soap tcp

我有一个演示肥皂服务,它回应了请求

 public class EchoWebService : SoapService
    {
        [SoapMethod("Echo")]
        public string Echo(string request)
        {
            Console.WriteLine("Request: {0}", request);
            return DateTime.Now + Environment.NewLine + request;
        }
    }

使用TCP托管服务

Uri to = new Uri("soap.tcp://localhost:5696");
EndpointReference EPR = new EndpointReference(to);
SoapReceivers.Add(EPR, new EchoWebService());

我可以通过编写自己的TCP SoapClient来调用服务,

Uri to = new Uri("soap.tcp://localhost:5696");
EndpointReference EPR = new EndpointReference(to);
mysoapclient client1 = new mysoapclient(EPR);
Console.WriteLine(client1.GetResponse("Hello World"));

class mysoapclient : SoapClient
{
        public mysoapclient(EndpointReference endpoint) : base(endpoint) { }
        public string GetResponse(string request)
        {

            return base.SendRequestResponse("Echo",
              request).GetBodyObject(typeof(string)).ToString();
        }
}

有没有办法使用TcpClient调用服务?我想通过TCP发送原始xml来调用服务并从网络流中读回响应。我尝试使用以下代码和xml,但它似乎不起作用。

TcpClient client = new TcpClient("localhost", 5696);
byte[] request = Encoding.UTF8.GetBytes(xml);
NetworkStream stream = client.GetStream();
stream.Write(request, 0, request.Length);
byte[] response = new byte[client.ReceiveBufferSize];
client.GetStream().Read(response, 0, client.ReceiveBufferSize);
Console.WriteLine(Encoding.UTF8.GetString(response));

<?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://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Echo xmlns="http://tempuri.org/">
      <request>Hello World</request>
    </Echo>
  </soap:Body>
</soap:Envelope>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要将所有二进制标头填充到SOAP/TCP

请参阅SUN's SOAP/TCP reference

作为提示,您可以运行tcpdumpwireshark,记录SOAP个会话,看看您需要填写什么。

要记录环回流量,请使用tcpdump将其捕获到文件中,然后使用wireshark

打开该文件