✅ MAUI on Android - problem with HttpClient Post Method
Hello guys, i am creating this post after more than week of learning and trying to solve it by myself.
I have problem with reaching my WebApi with Post Method. There is no problem with Get Method.
The error i'm getting is: System.ObjectDisposedException: 'Cannot access a closed Stream.'
Which makes literally 0 sense for me. (i dislike that C# took away from us destructors and that may be consequence of it ?)
Here is code
I will add full git project if needed. There is no problem with implementation of Login in API (tested with same code run on Windows and using Postman)
183 Replies
Here comes the repo
https://github.com/Badboy-2/MAUI_HttpClient
GitHub
GitHub - Badboy-2/MAUI_HttpClient: Showcase of problem i encountere...
Showcase of problem i encountered using HttpClient in MAUI - Badboy-2/MAUI_HttpClient
Where in the code you are getting the exception? There is a lot of things that can be disposed here...
@Stefanidze
In MainPage.xaml.cs line 66
or just
it skips to 'return 0;' then throws System.ObjectDisposedException: 'Cannot access a closed Stream.'
Everything is in same context... i don't really understand what is happening
Image just to portray, and also values from debugger
they are disposed too early
but how come, they are
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I know, but i was trying to set up perfectly clear code. I created a project, so everyone could fork, and try to run this on andro emulator or take function as whole
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
it's not main code
and no, it won't
Have you tried?
already tried to make it only thing in program
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
i tried that man
it doesn't work, my english is bad or what
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Because i tired to show that it doesn;t work in any method
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Okay
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
that's only one of approaches i know it's utterly terrible
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
kk, give me like 20 minutes and i will clean it up
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Maui is bug as whole
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Yeah, but it only doesn't work on post method
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
you could put get into that bullshit and it works
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
put GET method*
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
@TeBeCo did it
Again the link is:
https://github.com/Badboy-2/MAUI_HttpClient/tree/master/MAUI_HttpClient
GitHub
MAUI_HttpClient/MAUI_HttpClient at master · Badboy-2/MAUI_HttpClient
Showcase of problem i encountered using HttpClient in MAUI - Badboy-2/MAUI_HttpClient
The middle one with suffix _working still works, others do not
It doesn't even try to execute reading from response, object are just dying
Did you enabled cors on the api?
I don't see this problem related to maui tho
Sorry... did i enable what ? Do you want me put WebAPi project to git ?
Wait...
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Also i think it would be sus, if it would be problem on API site, bc it works on windows emulator and swagger
postman also
Literally below you have version you desire
which also doesnt work
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
YES
I usually add this on my webapi builder but I bet this method is risky. Ask @TeBeCo
for showcase
It's not duplicated bc it works diffrent way
i don't have anything working, so it's good to try like ... 2 options at once ? not only one ?
Because it accepts any method anywhere
Is this wrong approach ?
Will try that, give me sec
Its good for learning how webapi works. which i still do
@TeBeCo helps a lot of devs here and I'm sure he already forgets that he helped me before
Thanks again tho
Is it duplication if it works diffrently but result is same ? I mean i know in PROD it is, but this isn't PROD. Should i just go with one instead of 2 ? I thought showcasing is somehow good ?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
No problem 🙂 Take your time, goodluck also
System.ObjectDisposedException: 'Cannot access a closed Stream.'
Stills pops up, but i noticed
NetworkSecurityConfig] No Network Security Config specified, using platform default
Does it work on a non-maui project?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I added
Here
Stack Overflow
No Network Security Config specified, using platform default - Andr...
I am trying to make a database via 000webhost.com. I keep getting this message showing in the event log whenever I run the app from android studio. Does anyone knows how to solve this problem? Much
Open it with xml text editor
Does this also shows when you didn't add cors?
Yes
This one?
Try this out
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
MAUI only does webview if you're doing say blazor over maui
But its still native
or when u you to auth against oauth
Just the view isn't
we are talking about the need of CORS
Oh so I guess it doesn't need cors
Brb dinner
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
No :/ you will have to put your ip in API config files
is it acceptable ?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Okay, i'm on it
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Thank you guys for your time, i really do appreciate it
Have you tried this?
Yea, mine AndroidManifest.xml looks like this
wait what
I added
The one i cloned looked different on yours but try to add this
It is there
Where?
I accidentally pasted code from my opened copy of this project
GitHub
MAUI_HttpClient/MAUI_HttpClient/Platforms/Android/AndroidManifest.x...
Showcase of problem i encountered using HttpClient in MAUI - Badboy-2/MAUI_HttpClient
Oh it's in the "branch" branch
yea, im stupid
Did it worked?
No 😦
But i believe it was necessary step whe had to took. We have to kinda rip all security apart, to see whats going on imho
@Badboy you also need to add
To your csproj because u have a lot of missing libraries being loaded
https://github.com/Badboy-2/MAUI_HttpClient
https://github.com/Badboy-2/WebAPI_ForMaui
Replace this string in both project
192.168.0.175
with your ip (Ctrl+H ready, 0 risk involved :p)
Additionaly i think you have to add exceptions to firewall for port 5297 and 7258 / turn off firewall
GitHub
GitHub - Badboy-2/MAUI_HttpClient: Showcase of problem i encountere...
Showcase of problem i encountered using HttpClient in MAUI - Badboy-2/MAUI_HttpClient
GitHub
GitHub - Badboy-2/WebAPI_ForMaui
Contribute to Badboy-2/WebAPI_ForMaui development by creating an account on GitHub.
Thanks! Will add it in moment
This powershell comamand will create exceptions in your firewall
UDP too ? :andyHmm:
@Badboy your problem have nothing to do with
usesCleartextTraffic
because that is used when you're making a non-https connection but you're trying to access https.
On top of that you want to access a localhost certified ssl, so if you wrap the httpclient with a try and catch u will most likely get a Trust anchor for certification path not found.
error that backs that up
what you would have to do is create a custom android message handler with your internal api ip
i.e.:
then you would need to add that handler to your httpclient i.e.:
so in my case my api internal ip is 10.0.0.1
in your case you have to update it to yoursUnknown User•10mo ago
Message Not Public
Sign In & Join Server To View
@TeBeCo I mean u wouldn't need any of this if u had the api was else where
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
u also dont need any of this for local
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
u can't target iOS afaik u need a device
but I haven't start doing iOS so dont take my word for it
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
just change your targat to android
I can't read a thing in your video sorry
its too small
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
that would be WSL Android not google emulator AFAIK
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
mmm weird mine doesnt say WSA
maybe because ur on w11?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I dont have that
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
so I guess vs uses a completely different thing for android on w11
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
emulator is pretty fast for me never had issues
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
yeah I dont disagree
I never had the change to try wsa so I dont know how different it is unhappily
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I honestly dont know how it runs with WSA
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
with emulator it starts everything for u
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
that would have been a bliss if the emualtor had that
ah ok it recognizes it by default too nice
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
too bad they taking it down I wonder how fast it is too
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
that looks about the same as the emulator thou
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
no I mean the ui
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
no, I mean the screenshot above is the same app u use on either wsa or the emulator to create the device
its not different
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
mm I just use 4096 there instead
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
let me chck
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
ah no on that one
it was default to
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
ah
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
it does allow for both
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I dont have a mic for it but yeah your right it doesn'tr accept G never paid attention to it I never had to mess with that beyond creating the device
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Deploy: 0 succeeded, 0 failed, 1 skipped?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I dont know let me clone his repo to try
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
but u originally loaded the last project he sent in right
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
sure but it builds without doing anythng thou
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
well it all works here to deploy too
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
im latest preview everything it all run without touching anything
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
sure, and all I am saying is that I didn't have to do anything to run it, so I dont know why u had to vs that
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I dont have net7 either
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
that's why I dont understand why ur having such issues
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
since the answer was burry, @Badboy just for clarity I provided the solution from there so u can read it: https://discord.com/channels/143867839282020352/1221703280204451932/1221830944231657603
leowest
<@1221689581276106802> your problem have nothing to do with
usesCleartextTraffic
because that is used when you're making a non-https connection but you're trying to access https.
On top of that you want to access a localhost certified ssl, so if you wrap the httpclient with a try and catch u will most likely get a Trust anchor for certification path not found.
error that backs that upQuoted by
<@1102729783969861782> from #MAUI on Android - problem with HttpClient Post Method (click here)
React with ❌ to remove this embed.
How come you think so ? In LAN firewall rules do apply still. Am i missing something ? Or did you mean local - like in local device (not Local network)
yeah my bad if your api is on a different pc then yes you might need it, for local u dont
but either way u can place the sealed class in its own file in the android platform folder
and then use from the main code and it should work
Oh got it, thank you guys soo much for your intel. I appreciate, tomorrow morning( in like 10 hours) i will sit to this after some sleep. Even if it won't work, i still learnt a huge deal of things 🙂 For that i am grateful
I mean I tried it myself so I hope it works 😉
I did not try it from a machine within my local network but assuming u can access it via the browser u should be able to reach it just fine from the app
the problem is that it expects the url to be localhost
because its the certificate
but its not so you need to further add the hostname verification to your local ip
I also strongly recommend u to wrap those under
#if DEBUG
as well u dont want that to acciddentally land on the final product@leowest Thank you very much 🙂 it DOES WORK and for SSL also ;_; i tried to use nonSSL connections bc of marathon-long threads i saw about this problem on forums (it said SSL version mismatch makes it impossible to connect, thus using nonSSL)
I kinda don't believe still, after all this time. You guys really make it.
To everyone involved
@TeBeCo Also i will try your code tonight on diffrent platform (i truly do not want to mess up what is working)
(and i'm afraid of installing newer versions of .NET, 99% i'm just dumb not doing it right away, but caution is too strong in me)
(so i will do it firstly not on my DevEnv)
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
Yeah sure, good idea 😄
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
names of files ?
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
or objects etc.
okay
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View
I come from C++, and was pretty sure, that underscore is safe
Good to know it isn't
Do you perhaps have any clue why could underscore mess things up ? The idea of underscore was to not mess things up, i believe
Like parser thing, or smtn ?
no worries, yeah it was not simple because the output window was not giving any useful information
once I wrapped the error in a try catch it gave me a complete different error message which was what lead me to the answer
the only thing the output spotted was that u needed to embed the libraries into the apk
nevertheless glad it worked for u 🙂
$close
Use the /close command to mark a forum thread as answered