HTTP requests?
Getting error that the platform is not supported. Using a console project. Is this on purpose? I believe most apps can make HTTP requests no issue
Yes they can. And so can this app. I'm not sure what is going wrong without further data. Where is the PlatformNotSupportedException originating from?
Sometimes this just doesn't work, I've tried two apps that use HttpClient, I didn't get the same error but I have an error message for this
System.Net.Http.HttpRequestException: Connection failure
at Xamarin.Android.Net.AndroidMessageHandler+<>c__DisplayClass137_0.<ConnectAsync>b__0()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task+<>c.<.cctor>b__281_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at Xamarin.Android.Net.AndroidMessageHandler+<DoProcessRequest>d__140.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[[System.Net.Http.HttpResponseMessage, System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].GetResult()
at Xamarin.Android.Net.AndroidMessageHandler+<DoSendAsync>d__131.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[[System.Net.Http.HttpResponseMessage, System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].GetResult()
at System.Net.Http.HttpClient+<<SendAsync>g__Core|83_0>d.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.TaskAwaiter`1[[System.Net.Http.HttpResponseMessage, System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].GetResult()
at Polbsener.AkademikClient+<SendRequest>d__19.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.TaskAwaiter`1[[System.Net.Http.HttpResponseMessage, System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].GetResult()
at Polbsener.AkademikClient+<GetSessionToken>d__13.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.TaskAwaiter`1[[System.Net.Cookie, System.Net.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].GetResult()
at Polbsener.AkademikClient+<Login>d__14.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
at System.Runtime.CompilerServices.TaskAwaiter`1[[System.Boolean, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetResult()
at Polbsener.MainView+<OnSignInBtn_Clicked>d__12.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_0(Object state)
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0()
at Java.Lang.Thread+RunnableImplementor.Run()
at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz)
I found this as well but I don't know how to apply it in the IDE https://stackoverflow.com/questions/75616770/xamarin-forms-failed-to-resolve-system-net-http-httpclienthandler
I once came across this same error "connection failure" while making http requests.This error can be as a result of a wrong url for example; if you are using this type of ulrl "http://www.website.com" you will need to change it to "https://www.website.com" and try again.
I got the right URL, the code I have is copied from my desktop project and was ported to mobile, and it only doesn't work on mobile
The code is always called when making requests is this
public async Task<HttpResponseMessage> SendRequest(HttpMethod method, string uri, CookieContainer? cookieContainer = null, string? referrer = null, HttpContent? body = null)
{
using var handler = new HttpClientHandler();
handler.UseCookies = true;
if (cookieContainer != null)
handler.CookieContainer = cookieContainer;
using (var client = new HttpClient(handler))
{
if (client == null) return new HttpResponseMessage(HttpStatusCode.UnprocessableEntity);
if (referrer != null)
client.DefaultRequestHeaders.Referrer = new Uri(referrer);
var message = new HttpRequestMessage(method, uri);
if (body != null)
message.Content = body;
var response = await client.SendAsync(message);
return response;
}
}
I found an unsafe workaround for this
I added this in my .csproj
<UseNativeHttpHandler>false</UseNativeHttpHandler>
and I also added this in the code
using var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
@ReDarkTechnology UseNativeHttpHandler: false wouldn't do anything in this app right now.
So this is an SSL issue? .NET Android has had regressions in that since .NET 6 (Xamarin.Android worked). Doesn't seem Microsoft will be fixing this then so that workaround may be "official" for a lot of apps. I will experiment to see if I can make UseNativeHttpHandler optional and if it is enough to fix the issue
Yeah I tried this in Visual Studio, not sure if it would actually work in the app, but I'd love to test if you finished implementing it