views:

672

answers:

1

I have a demo soap service which echos back the request

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

Service is hosted using TCP,

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

I can invoke the service by writing my own 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();
        }
}

Is there any way to invoke the service using TcpClient? I want to send the raw xml over TCP to invoke the service and read the response back from the network stream. I tried using following code and xml but it doesn't seem to work.

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/"&gt;
  <soap:Body>
    <Echo xmlns="http://tempuri.org/"&gt;
      <request>Hello World</request>
    </Echo>
  </soap:Body>
</soap:Envelope>

Any help would be appreciated.

+1  A: 

You need to fill all binary headers to SOAP/TCP.

See SUN's SOAP/TCP reference.

As a hint, you may run tcpdump or wireshark, record you SOAP session and see what exactly you need to fill.

To record loopback traffic, use tcpdump to capture it to the file, and open the file with wireshark

Quassnoi
i tried using wireshark (along with all the hacks mentioned @ http://wiki.wireshark.org/CaptureSetup/Loopback) but it doesn't capture loopback traffic on windows. tcpdump captured the loopback traffic but output wasn't meaningful.
chikak
I took a dump with tcpdump and opened the file in wireshark. When i followed the tcp stream i could only see following, . ...-.)....urn:uuid:406356e4-dc0f-4fb7-8dMIME-Version: 1.0Content-Type: multipart..@..........
chikak