Receice Soap Exceptions

Discussion in 'ASP .Net Web Services' started by Anbu, Aug 10, 2006.

  1. Anbu

    Anbu Guest

    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
    Anbu, Aug 10, 2006
    #1
    1. Advertising

  2. Anbu

    Anbu Guest

    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;
    }
    Anbu, Aug 10, 2006
    #2
    1. Advertising

  3. Anbu

    Anbu Guest

    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
    Anbu, Aug 21, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maat [EBL]

    Web Methods & SOAP Exceptions

    Martin Maat [EBL], Feb 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    Martin Maat [EBL]
    Feb 2, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,012
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,014
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    607
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    612
Loading...

Share This Page