简体   繁体   中英

xamarin android : httpclient PostAsync

we have an app under xamarin android build with visual studio 2017. this app works since three years without any problems.

since two weeks and I don't know why actually some device can't sync with our back end. It's really strange because nothing has change in this part.

this error does not appear on all devices but on one or two from time to time we use the dll httpClient for to sync the datas with our backend.

If i put a break point inside the postAsync I have an exception with this -> Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream

Any one has an idea about how to solve this? also what does it meam?

Here is it the code of the postAsync method: thanks for our time and comment guys

public override HttpResult ExecutePost(Uri target, string body)
    {
        var client = new HttpClient();
        client.MaxResponseContentBufferSize = MaxHttpResponseBufferSize;

        try
        {
           var requestContent = new StringContent(body, RequestContentEncoding, RequestContentType);
           var response = client.PostAsync(target, requestContent).Result;
           if (response.IsSuccessStatusCode)
           {
              var content = response.Content.ReadAsStringAsync().Result;
              return new HttpResult(content, null, null);
           }

           return new HttpResult(null, "Response is empty", response.StatusCode.ToString());
        }
        catch (Exception e)
        {
            return new HttpResult(null, "Problem with the HttpPost", e.Message);
        }
    }

thanks for the link your provide. I've try up the buffer on the postasync / try to sync in wifi OR 3G / delete special character in json /... but nothing work

we have move the prod database to the test and try to sync the data to the test database with postman. with postman the result was ENTITY TOO LARGE !

Json is size > 1.2 mega and the default value inside IIS is set to 1 mega

Here is it the problem...

thanks problem solve

I experienced the same issue. Have been battling for 6 hours on this issue.

If you read the error, I was getting (Failed to connect to localhost/127.0.0.1:7113). If you put localhost in your browser or swagger tool it will work but if you put https://127.0.0.1:7113/api/weatherforecast in your browser it will not work. It will give you a certificate problem.

So I think you have to resolve 127.0.0.1 to localhost with https certificate on your local dev machine.

I'm building a MAUI app with Visual Studio 2022 Preview.

So I solved this issue by deploying my API to AZURE.

Then update to the azure url for example: string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";

and then it worked brilliantly. Like super brilliantly.

Here is my code:

 public void LoginAsync()
 {
        HttpClient client = new HttpClient();

        string apiUrl = "https://weatherforecast.azurewebsites.net/api/weatherforecast";

        UserCredentials.EmailAddress = LoginUIEntity.EmailAddress;
        UserCredentials.Password = LoginUIEntity.Password;

        string serialized = JsonConvert.SerializeObject(UserCredentials);

        var inputMessage = new HttpRequestMessage
        {
            Content = new StringContent(serialized, Encoding.UTF8, "application/json")
        };

        inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            var message = client.PostAsync(apiUrl, inputMessage.Content).Result;

            if (message.IsSuccessStatusCode)
            {
                var apiResponse = message.Content.ReadAsStringAsync();
                UserCredentials = JsonConvert.DeserializeObject<UserCredentials>(apiResponse.Result);

                if (UserCredentials.IsValid)
                {
                    UserCredentials.IsLoggedIn = true;
                }
                else
                {
                    ErrorMessage = "Invalid credentials supplied.";
                }
            }
        }
        catch (Exception ex)
        {
           ErrorMessage = "An error has occurred. Please contact support if the error persists.";
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM