c# - 发送 Soap 请求并捕获响应

标签 c# wpf web-services soap

我正在尝试构建 WPF 程序以根据作为服务引用添加的 WSDL 创建 Soap 请求作为 xml 文件。
问题是我无法将代理类配置为使用该 xml 文件并将其作为请求发送以及接收响应。它给了我一个异常(exception):

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll Additional information: 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;
    }

最佳答案

向下编辑代码以忽略不与 web 服务调用交互的对象:

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

在不知道网络服务期望什么的情况下,它看起来就像您正在尝试保存一个全新的对象,该对象没有以任何方式更改(可能是也可能不是有效的事情)。你没有处理它抛出的异常,这就是它未被处理的原因 - 你可以简单地将它移动到 try block 的范围内,所以更像是:

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

关于c# - 发送 Soap 请求并捕获响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36445101/

相关文章:

c# - 如何在新进程中启动form2?

c# - double 数组

c# - 我可以更改用户的键盘输入吗?

c# - 使用 `DataContractSerializer` 序列化包含 WPF 画笔的类

WPF多线程绑定(bind)MVVM

Java 调用 Web 服务不更新 boolean 属性

访问 Web 服务时的 Android 应用程序安全性

c# - System.InvalidCastException 解析 FluentCommandLineParser 参数

.net - 在 Windows 8 下,触摸 WPF 按钮有时不会调用单击处理程序

java - JAXB 不为 List 创建 set 方法