Receice Soap Exceptions

A

Anbu

Hi All,

How to Receive Soap Exception using HTTP POST operation.

Here is the code that calls the Web Service

try
{
string strg = envelope.InnerXml;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(destination);
req.Method = "POST";
req.KeepAlive = true;
req.ContentType = "text/xml";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strg);
req.ContentLength = bytes.Length;
req.Headers.Add("SOAPAction: " + SoapAction);

System.IO.Stream st = req.GetRequestStream();
st.Write(bytes, 0, bytes.Length);
st.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
System.IO.Stream st1 = res.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(st1,
System.Text.Encoding.UTF8);
string txt = sr.ReadToEnd();
SoapEnvelope response = new SoapEnvelope();
response.LoadXml(txt);
//this.txtSOAPResponse.Text = txt;
res.Close();
//Console.WriteLine(response.Envelope.OuterXml);
return response;
//return true;
}
catch (SoapException soapexp)
{
SoapEnvelope responseexp = new SoapEnvelope();
responseexp.LoadXml(soapexp.Message);
return responseexp;
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
Console.WriteLine(exp.StackTrace);
SoapEnvelope responseexp = new SoapEnvelope();
responseexp.LoadXml(exp.Message);
return responseexp;
}

Using above method does not return the SoapFault or SoapException. How
can I capture the SoapException thrown by the web service?

TIA
 
A

Anbu

I found the way to return the error as soap exception.

WebException has the info as Response object. Here is the code to
process the response

catch (WebException exp)
{
Console.WriteLine(exp.Message);
Console.WriteLine(exp.StackTrace);
HttpWebResponse httpResponse = (HttpWebResponse)response;
System.IO.Stream stream = httpResponse.GetResponseStream();
System.IO.StreamReader readStream = new
System.IO.StreamReader(stream, System.Text.Encoding.UTF8);
string txt = readStream.ReadToEnd();
readStream.Close();
SoapEnvelope response = new SoapEnvelope();
response.LoadXml(txt);
logger.Debug("Response Converted into SoapEnvelope");
logger.Debug(txt);
logger.Debug(soapResponse.InnerText);
httpResponse.Close();

Console.WriteLine(response.Envelope.OuterXml);
return false;
}
 
A

Anbu

Gaurav,

Thanks for your info.

But the WebException's Response object holds the SoapException raised
by the server as text/html or text/xml formats. There could be some
more formats, but I'm not bothered about these. When I receive the
response with text/xml format, I could always parse the soapenvelope
and understand the data.

Thanks again for your info
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top