HttpClient PostAsync behavior different in Xamarin.Android?

SachinAgrawalSachinAgrawal USUniversity ✭✭

I have a web api POST method, which takes 3 string parameters and returns a response. If there is some exception then the Raw message returned looks something like below. When in my Xamarin.Android project, I receive the response and try to parse to JSON, I get ONLY "Message" property. There is nothing else in the received response. But, if I try the same code(as in Android project) in winform or MVC project, I do get all the properties back in JSON format. I am particularly interested in the "ExceptionMessage" property here. Any thoughts?

{
"Message": "An error has occurred.",
"ExceptionMessage": "Application Name TEST not found. ",
"ExceptionType": "System.Exception",
"StackTrace": " ...",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "... InternalServerError Application Name TEST not found. ",
"ExceptionType": "System.Exception",
"StackTrace": null
}
}

Answers

  • CheesebaronCheesebaron DKInsider, University mod

    Post your code.

  • SachinAgrawalSachinAgrawal USUniversity ✭✭
    edited June 2015

    @Cheesebaron below is the code:
    private async void button1_Click(object sender, EventArgs e)
    {
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://localhost/api.host/core/");
    var requestUrl = "?appName=TEST&userName=username&password=Password123";
    var request = new StringContent("", Encoding.UTF8, "application/json");
    request.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await httpClient.PostAsync("api/client/register" + requestUrl, request);
            if (!response.IsSuccessStatusCode)
            {
                JObject obj = JObject.Parse(response.Content.ReadAsStringAsync().Result);                
                string test = (string)obj["ExceptionMessage"];              
                return;
            }
        }
    
  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    @Cheesebaron The web api method would throw an exception as below when an error happens like below:

    throw new ApplicationException(string.Format("Application Name {0} not found.", model.AppName));

  • CheesebaronCheesebaron DKInsider, University mod

    Plaintext username and password as parameters? Nice...

    Anyways, try acutally await the reading of the json string. Calling .Result is really, really bad practice...

    You are using await further up, why block the entire app later?

  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    I already tried that and same issue.

    string result = await response.Content.ReadAsStringAsync();
    JObject obj = JObject.Parse(result);

  • CheesebaronCheesebaron DKInsider, University mod

    Where are you printing the result, or are you just using a debugger to inspect the JObject?

  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    using debugger. string result = await response.Content.ReadAsStringAsync();

    The variable result just has the property "Message"

  • CheesebaronCheesebaron DKInsider, University mod

    You probably need to inspect the traffic using something like Fiddler or similar.

    Could you try install ModernHttpClient from nuget and see if it makes a difference?

    The only code change would be:

    var client = new HttpClient(new NativeMessageHandler());
    
  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    @Cheesebaron Don't see any activity in Fiddler when the android app is executed, but I do see the call made to web api when my winform project runs.

    Also, I changed to ModernHttpClient, but same issue.

  • CheesebaronCheesebaron DKInsider, University mod

    You need to set up a proxy for your device in order to get stuff appearing in fiddler.

    Did you try to make a Class which you can deserialize the response into instead?

  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    yes, i created a custom class to deserialize the response, but nothing gets hydrated except the "Message" property

  • CheesebaronCheesebaron DKInsider, University mod

    I have never seen this issue and all json responses I get in my apps are all OK. Are you really sure you get the entire response?

  • SachinAgrawalSachinAgrawal USUniversity ✭✭

    yes, because if i use fiddler "composer" tab to execute the exact url for web api, then i do see the "ExceptionMessage" property in there. But, doesn't come through in the httpresponse in Xamarin.Android project.

  • RenaudRenaud FRMember ✭✭

    Hi,
    I get exactly the same issue.
    Do you find a solution ?

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭
    edited July 2018

    @SachinAgrawal said:
    I have a web api POST method, which takes 3 string parameters and returns a response. If there is some exception then the Raw message returned looks something like below. When in my Xamarin.Android project, I receive the response and try to parse to JSON, I get ONLY "Message" property. There is nothing else in the received response. But, if I try the same code(as in Android project) in winform or MVC project, I do get all the properties back in JSON format. I am particularly interested in the "ExceptionMessage" property here. Any thoughts?

    {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Application Name TEST not found. ",
    "ExceptionType": "System.Exception",
    "StackTrace": " ...",
    "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "... InternalServerError Application Name TEST not found. ",
    "ExceptionType": "System.Exception",
    "StackTrace": null
    }
    }

    Please Follow these tutorial.

    https://c-sharpcorner.com/article/developing-restful-api-in-asp-net-with-add-method/

    https://c-sharpcorner.com/article/developing-restful-apis-in-asp-net-with-retrieve-update-and-delete-functions/

    https://c-sharpcorner.com/article/consuming-restful-apis-in-xamarin-android/

Sign In or Register to comment.