getting soap request and response in xml

3.2k Views Asked by At

we are working with UPS shipment API and there are certain issues we are facing. After contacting UPS technical support, they have asked to provide them with the SOAP envelope (Request/Respose) in xml format.

Kindly assist that how can that be acquired from the code . below is the service call to UPS API.

ShipmentResponse shipmentReponse =
                     shipService.ProcessShipment(shipmentRequest);

any help appreciated.

1

There are 1 best solutions below

1
On

If you want to do this from the program itself, you can add an endpoint behaviour, assuming you're using WCF, to save the soap request and response.

Usage would be something like this,

using (ChannelFactory<IService> scf = new ChannelFactory<IService>(new BasicHttpBinding(), "http://localhost:8000/Soap"))
{
    scf.Endpoint.EndpointBehaviors.Add(new SimpleEndpointBehavior()); // key bit
    IService channel = scf.CreateChannel();

    string s;
    s = channel.EchoWithGet("Hello, world");

    Console.WriteLine("   Output: {0}", s);
}

It is described here in detail here: http://msdn.microsoft.com/en-us/library/ms733786%28v=vs.110%29.aspx, the key methods for you are AfterReceiveReply and BeforeSendRequest, where you can store or save the SOAP xml as necessary.

// Client message inspector
public class SimpleMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        // log response xml
        File.WriteAllText(@"c:\temp\responseXml.xml", reply.ToString());
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
    {
        // log request xml
        File.WriteAllText(@"c:\temp\requestXml.xml", request.ToString());
        return null;
    }
}

public class SimpleEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
        // No implementation necessary
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new SimpleMessageInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        // No implementation necessary
    }

    public void Validate(ServiceEndpoint endpoint)
    {
        // No implementation necessary
    }
}