在xml中获取soap请求和响应

时间:2014-12-21 09:45:35

标签: c# xml soap ups

我们正在使用UPS货件API,我们正面临一些问题。在联系UPS技术支持后,他们要求以xml格式向他们提供SOAP信封(请求/休息)。

请协助如何从代码中获取。以下是UPS API的服务电话。

ShipmentResponse shipmentReponse =
                     shipService.ProcessShipment(shipmentRequest);

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

如果要从程序本身执行此操作,可以添加端点行为(假设您正在使用WCF)来保存soap请求和响应。

用法就是这样,

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);
}

此处详细介绍:http://msdn.microsoft.com/en-us/library/ms733786%28v=vs.110%29.aspx,关键方法是AfterReceiveReplyBeforeSendRequest,您可以根据需要存储或保存SOAP xml。

// 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
    }
}
相关问题