发送soap请求并捕获响应

问题描述:

您好,

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

mscorlib.dll中出现未处理的类型'System.ServiceModel.FaultException`1'的异常附加信息:应用程序错误



公共字符串returnSerializedxml(对象输入)

{

XmlSerializer xmlSerializer = new XmlSerializer(input.GetType());



using(StringWriter textWriter = new StringWriter())

{

xmlSerializer.Serialize(textWriter,input);

返回textWriter.ToString();

}

}



private void button1_Click(object sender,RoutedEventArgs e)

{

Consi gnmentEndpointClient proxy = new ConsignmentEndpointClient();

save sv = new save();

saveResponse response = new saveResponse();

XmlDocument doc = new XmlDocument();

doc.Load(PATH);

response = proxy.save(sv); / *这里发生异常* /



尝试

{

Output.Text =回复:\\ \\ n+ returnSerializedxml(response);

}

catch(异常错误)

{

Output.Text =请求中出错:\ n+错误;

}



我尝试过:



我不确定但是像FileStream或者类似的东西丢失了,我会感谢任何提示。

Hi there,
Iam trying to build WPF program to create Soap requests as xml files according to the WSDL which is added as service reference, 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 .it gives me an 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;
}

What I have tried:

Iam not sure but something is missing like FileStream or similar, I would be thankful for any hint .

尝试下面的代码,如果我正确理解你的查询。

将你的xml传递给Soapcommand



Try below code, If I understand your query correctly.
pass your xml to Soapcommand

private string ProcessRequest(string SoapCommand)
   {
       string soapResult = string.Empty;
       try
       {
           WebRequest webRequest = CreateWebRequest();
           XmlDocument soapEnvelopeXml = new XmlDocument();
           soapEnvelopeXml.LoadXml(SoapCommand);


           using (Stream stream = webRequest.GetRequestStream())
           {
               soapEnvelopeXml.Save(stream);
           }

           using (WebResponse response = webRequest.GetResponse())
           {
               using (StreamReader rd = new StreamReader(response.GetResponseStream()))
               {
                   soapResult = rd.ReadToEnd();
               }
           }
       }
       catch (Exception)
       {
           throw;
       }
       return soapResult;
   }

   public static HttpWebRequest CreateWebRequest()
   {
       HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://111.11.31.116:45259/ilws/LinkSOAP");
       webRequest.Headers.Add(@"SOAP:Action");
       webRequest.ContentType = "text/xml;charset=\"utf-8\"";
       webRequest.Accept = "text/xml";
       webRequest.Method = "POST";
       return webRequest;
   }