0

I use Xamarin.Forms and I run my application on the Android-simulator and on my mobile device

When I try to send HttpRequestMessage, my application is turned off. This happens in the SendRequest method, but sometimes I get An error occurred while sending the request

I also created a console application, tested the request there and everything works, but Android does not.

public async Task<TResult> PostAsync<TResult>(string uri, IRequestData data, string token = "", string header = "")
        {
            var request = new HttpRequestMessage(HttpMethod.Post, uri);
            request.AddAuthoriationHeader(token);

            var content = data.ToJson();
            request.SetContent(content, ContentTypes.Json);

            var response =  await SendRequestAsync(request);
            var result = await HandleResponseAsync<TResult>(response);

            return result;
        }

public static void SetContent(this HttpRequestMessage request,
            string content,
            ContentTypes contentType)
        {
            request.Content = new StringContent(content,
                Encoding.UTF8,
                contentType.Value());
        }

public static string Value(this ContentTypes contentType)
        {
            switch (contentType)
            {
                case ContentTypes.XxxForm:
                    return "application/x-www-form-urlencoded";
                case ContentTypes.Json:
                    return "application/json";
                default:
                    throw new ArgumentOutOfRangeException(nameof(contentType), contentType, null);
            }
        }

private async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage request)
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        return await client.SendAsync(request); // Shut down on this line
                    }
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }

What is it can be and why the conversion in the console to the local host is working, but with the phone and emulator error?

1

1 Answer 1

1

Are you actually using localhost in the address?

Then you should change that to the IP address on the network of your machine. When running the test console application, you are running from the same machine your server application is running, so localhost is still localhost from that machine.

When running this from an app, the device (even emulated) will then be the localhost and the server application isn't running on that device or emulator. If you want to test from a device or emulator, find the IP of your development machine (or any other machine your server application is running on) and use that as the address. This address will probably look something like: 192.168.x.x or 10.x.x.x.

Depending if you are on Windows or Mac, open a console window and type ipconfig for Windows and ifconfig for Mac. You should be able to get your IP address from there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.