开发者

MVC RESTFUL HTTPClient try catch oddity

开发者 https://www.devze.com 2023-04-09 05:02 出处:网络
I\'m a little puzzled why my try...catch exception does not seem to work properly. I set up the following code which works fine - to call a WCF restful service. I decided to test it by shutting off th

I'm a little puzzled why my try...catch exception does not seem to work properly. I set up the following code which works fine - to call a WCF restful service. I decided to test it by shutting off the WCF restful service in hopes that it will trap error messages or ignore a 200(ok) message etc but for some reason..the try catch and response.EnsureStatusIsSuccessful() seems to be ignored thus generating a HttpStageProcessingException error in the middle of the try catch..

As I understand it, I should only need to catch the ArgumentOutOfRangeException when a 200 is ignored...thoughts?

        // set up passing parameters
        var p = new HttpQueryString();
        p.Add("username", username);
        p.Add("password", password);

        try
        {
            WebConfigurations cfg = new WebConfigurations();
            HttpResponseMessage response;

            // cfg - gets the base address for the URL of the WCF
            using (HttpClient client = new HttpClient(cfg.GetBaseUrl()))
            {
                // exception HttpStageProcessingException occurs here...
                response = client.Get(new Uri(client.BaseAddress + "MainService/ValidateUser"), p);
                response.EnsureStatusIsSuccessful();

                string memberallowed = string.Empty;

                // process the resonse xml etc..NOT a problem...
                ProcessStreamData proc = new ProcessStreamData();
                memberallowed = proc.ReadStreamData(response);

                // blah balh
                return result;
          开发者_JAVA技巧  }

        }

        catch (ArgumentOutOfRangeException aorEx)
        {

            Console.Write("Argument Out of Range: " + aorEx.Message);
            return false;

        }
        // This really should not be here??? No error message is generated, too...
        catch (HttpStageProcessingException timeEx)
        {
            Console.Write("Stage Processing" + timeEx.Message);
            return false;

        }
        catch (Exception ex)
        {
            Console.Write("Standard error" + ex.Message);
            return false;
        }


Have you tried doing this after telling Visual Studio not to break on thrown exceptions? From the Menu, Debug-> Exceptions and then uncheck thrown.

Also, you probably want to dump that version of HttpClient from the Rest Starter Kit. The latest version can be found here http://nuget.org/List/Packages/HttpClient and be warned there are breaking changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消