Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Get full WebResponse in case of error when calling webRequest.GetResponse();

Posted on by 2,983

AX 2012, I have this code that returns catches any error during a web request:

 try{
        httpWebResponse = webRequest.GetResponse();
    }
    catch (Exception::CLRError)
    {
         ex = ClrInterop::getLastException();
        if (ex != null)
         {
            ex = ex.get_InnerException();
            if (ex != null)
            {
                error(ex.ToString());
            }
        }
        return;
    }
/*
Also tried
catch(Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }

*/


So this is working OK, but I'm justing getting something generic like "server returned error 500"

Is there any way I can get the full XML Response from the web service I'm calling. The thing is that the exact detailed error will be in the full response body.

Any ideas?

*This post is locked for comments

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    First of all, let me format your code, so we can read it more easily. Please use Insert > Code (in the rich-formatting view) next time.

    try
    {
    	httpResponse = httpRequest.GetResponse();
    
    	ResponseStream = httpResponse.GetResponseStream();
    	readStream = new System.IO.StreamReader(ResponseStream);
    	strResponse = readStream.ReadToEnd();
    	info(strFmt("%1",strResponse));
    
    	httpResponse.Close();
    	readStream.Close();
    
    	CodeAccessPermission::revertAssert();
    }
    catch (Exception::CLRError)
    {
    	//error(AifUtil::getClrErrorMessage());
    	ex = ClrInterop::getLastException();
    	if (ex != null)
    	{
    		WebException = ex.get_InnerException();
    		httpResponse = WebException.get_Response();
    		ResponseStream = httpResponse.GetResponseStream();
    		readStream = new System.IO.StreamReader(ResponseStream);
    		strResponse = readStream.ReadToEnd();
    		error(strResponse);
    	}
    	httpResponse.Close();
    	readStream.Close();
    }

    Then please tell us more about what happens. Are you saying that an exception is thrown, but no code in the catch block gets executed? For example, if you uncomment code at line 17, no message is added to infolog?

    Isn't your code inside a transaction (ttsbegin/ttscommit)?

  • Suggested answer
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    Hello,

    Please help, I have this part of code, and I tried in all the possible way to catch the error when I have bad error 400 code, but it's not working for me. This is my try catch code, can you please help me to find what I'm typing wrong?

    try
    {
    httpResponse = httpRequest.GetResponse();

    ResponseStream = httpResponse.GetResponseStream();
    readStream = new System.IO.StreamReader(ResponseStream);
    strResponse = readStream.ReadToEnd();
    info(strFmt("%1",strResponse));

    httpResponse.Close();
    readStream.Close();

    CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
    //error(AifUtil::getClrErrorMessage());
    ex = ClrInterop::getLastException();
    if (ex != null)
    {
    WebException = ex.get_InnerException();
    httpResponse = WebException.get_Response();
    ResponseStream = httpResponse.GetResponseStream();
    readStream = new System.IO.StreamReader(ResponseStream);
    strResponse = readStream.ReadToEnd();
    error(strResponse);
    }
    httpResponse.Close();
    readStream.Close();
    }

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    Indeed the solution was to map webException to get_InnerException. Thanks for the ideas. This is my final code:

    try{
            httpWebResponse = webRequest.GetResponse();
        }
        catch (Exception::CLRError)
        {
            ex = ClrInterop::getLastException();
            if (ex != null)
            {
                WebException = ex.get_InnerException();
                Response = WebException.get_Response();
                stream = Response.GetResponseStream();            
                streamReader = new System.IO.StreamReader(stream);
                responseStr = streamReader.ReadToEnd();
                error(responseStr);
            }       
            return;
        }


  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    Yes, he already uses get_InnerException() in his originally code. By the way, your code can be greatly simplified, because the "as" operator can already handle what you've implemented manually with "is":

    ex = ex.get_InnerException();

    webException = ex as System.Net.WebException;

    if (webException)

    {

    .........

    }

    I apologize for not formatting the code properly, but the rich formatting view doesn't work for me at the moment.

  • Hariharans87 Profile Picture
    Hariharans87 4,971 on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    You can get the web exception by calling get_InnerException();

    System.Exception ex;

    .....

    catch (Exception::CLRError)

    {

       ex = CLRInterop::getLastException();

       if (ex != null)

       {

           ex = ex.get_InnerException();

           if ((ex != null) && (ex is System.Net.WebException))

           {

               webException = ex as System.Net.WebException;

               .........

           }

       }

    }

  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    getLastException() returns TargetInvocationException, not WebException. WebException may be inside the TargetInvocationException.

    Consider using the debugger to understand what's going on in your code.

  • Ghetz Profile Picture
    Ghetz 2,983 on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    Seems like I can't get it right. So far I'm trying this approach:

    System.Net.WebException WebException = null;

    System.Net.WebResponse Response;

    ...

    catch (Exception::CLRError)

    {

           WebException = ClrInterop::getLastException();

           Response = WebException.get_Response(); //THIS ASSIGNATION ENDS THE CODE EXECUTION, guess it's throwing some kind of error.

           stream = Response.GetResponseStream();

           //BP deviation documented

           streamReader = new System.IO.StreamReader(stream);

           responseStr = streamReader.ReadToEnd();

    }

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    RE: Get full WebResponse in case of error when calling webRequest.GetResponse();

    You display only the result of ToString() of the exception, but you surely can look at other properties of the exception.

    For example, cast the inner exception to WebException and then read properties such as Response.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans