App Running In Android Emulator Fails To Perform An Http Post To Localhost
Solution 1:
this might not be a direct answer to your question, but i would like to suggest localtunnel. a very easy way to temporarily expose your local api so that you can test it either on emulator or even physical device. Have used this alot my self, as it is very convenient to just type a single line in terminal to start it.
Solution 2:
The following reference solved my issue.
Infrastructure:
type GlobalHttpClient private () =
staticletmutable (httpClient:System.Net.Http.HttpClient) = nullstatic member val Instance = httpClient withget,set
Xamarin.Android project:
using Android.Http;
using Android.Net;
using Javax.Net.Ssl;
using System.Net.Http;
using Xamarin.Android.Net;
using Xamarin.Forms;
using WebGatewaySupport;
[assembly: Dependency(typeof(HTTPClientHandlerCreationService_Android))]
namespaceAndroid.Http
{
publicclassHTTPClientHandlerCreationService_Android : IHTTPClientHandlerCreationService
{
public HttpClientHandler GetInsecureHandler()
{
returnnew IgnoreSSLClientHandler();
}
}
internalclassIgnoreSSLClientHandler : AndroidClientHandler
{
protectedoverride SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
{
return SSLCertificateSocketFactory.GetInsecure(1000, null);
}
protectedoverride IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
{
returnnew IgnoreSSLHostnameVerifier();
}
}
internalclassIgnoreSSLHostnameVerifier : Java.Lang.Object, IHostnameVerifier
{
publicboolVerify(string hostname, ISSLSession session)
{
returntrue;
}
}
}
Xamarin.Forms App:
switch (Device.RuntimePlatform)
{
caseDevice.Android:
GlobalHttpClient.Instance = newHttpClient(DependencyService.Get<IHTTPClientHandlerCreationService>().GetInsecureHandler());
break;
default:
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) =>true;
GlobalHttpClient.Instance = newHttpClient(newHttpClientHandler());
break;
}
Client Gateway:
letpostTo (baseAddress:string) (resource:string) (payload:Object) =
GlobalHttpClient.Instance.BaseAddress <- Uri(baseAddress)
let encoded = Uri.EscapeUriString(resource)
let result = GlobalHttpClient.Instance.PostAsJsonAsync(encoded, payload) |> toResult
result
Solution 3:
Looks like you have a .NET Core Api. .NET Core does not have System.Web
in Asp.NET. The HttpPost
attribute and HttpGet
attributes should come from Microsoft.AspNetCore.Mvc
namespace which you have open.
Also since you are using the ApiController
attribute model binding will just work as long as you bind to a model and not just a json string.
Create a model that you want the json to bind to and use that type for your parameter on Post
and remove the FromBody
attribute. Also if you do that you probably don't need newtonsoft.json
.
open Microsoft.AspNetCore.Mvc
[<ApiController>]
[<Route("api/[controller]")>]
type RegisterController () =
inherit ControllerBase()
[<HttpPost>]
member x.Post(thing:TypeOfThing) =
Post a Comment for "App Running In Android Emulator Fails To Perform An Http Post To Localhost"