简体   繁体   English

发送肥皂请求并捕获响应

[英]Send Soap Request and capture the Response

I am trying to build WPF program to create Soap requests as xml files according to the WSDL which is added as service reference. 我试图构建WPF程序,以根据作为服务引用添加的WSDL将Soap请求创建为xml文件。
The problem is that I could not configure the proxy class to use that xml file and send it as a request as well as receiving the response. 问题是我无法将代理类配置为使用该xml文件并将其作为请求发送以及接收响应。 It gives me an exception: 它给了我一个例外:

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll Additional information: APPLICATION ERROR mscorlib.dll中发生了类型为'System.ServiceModel.FaultException`1'的未处理异常。其他信息:APPLICATION ERROR

public string returnSerializedxml(object input)
{
    XmlSerializer xmlSerializer = new XmlSerializer(input.GetType());

    using (StringWriter textWriter = new StringWriter())
    {
        xmlSerializer.Serialize(textWriter, input);
        return textWriter.ToString();
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    ConsignmentEndpointClient proxy = new ConsignmentEndpointClient();
    save sv = new save();
    saveResponse response = new saveResponse();
    XmlDocument doc = new XmlDocument();
    doc.Load(PATH);
    response= proxy.save(sv);  /*Here occur the exception*/

    try
    {
        Output.Text = "Response : \n" + returnSerializedxml(response);     
    }
    catch (Exception error)
    {
        Output.Text = "Error in Request : \n" + error;
    }

Editing the code down to ignore the objects that aren't interacting with the webservice call gives: 向下编辑代码以忽略不与Webservice调用交互的对象,可以得到:

ConsignmentEndpointClient proxy = new ConsignmentEndpointClient();
save sv = new save();
response= proxy.save(sv);  /*Here occur the exception*/

Without knowing what the webservice is expecting, it looks like you're trying to save a brand new object, that has not been changed in any way (which may, or may not, be a valid thing to do). 不知道Web服务的期望是什么, 似乎您正在尝试保存一个全新的对象,该对象没有进行任何更改(这可能是有效的事情,也可能不是)。 You're not handling the exception that it is throwing, which is why it is unhandled - you could simply move that to be within the call into the scope of the try block, so something more like: 您没有处理它引发的异常,这就是未处理它的原因-您可以将其简单地移动到try块范围内的调用中,因此更像:

private void button1_Click(object sender, RoutedEventArgs e)
{
    try
    {
        ConsignmentEndpointClient proxy = new ConsignmentEndpointClient();
        save sv = new save();

        throw new NotImplementedExcetpion("This probably needs to be associated with your sv object in some way, or just removed altogether");

        XmlDocument doc = new XmlDocument();
        doc.Load(PATH);
        saveResponse response = proxy.save(sv);  /*Here occur the exception*/

        Output.Text = "Response : \n" + returnSerializedxml(response);     
    }
    catch (Exception error)
    {
        Output.Text = "Error in Request : \n" + error;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM