Xamarin.Forms Android : access localhost (10.0.2.2) from WebView - android

I have a Xamarin.Forms mobile app, this one contains a WebView.
This WebView URL should be my local development machine because I have a web app running there.
So, when running the Android Emulator, I try to use the loopback address, https://10.0.2.2:44390 ... but the WebView remain white!
I have read the following docs, but it does not help:
https://learn.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services
https://medium.com/#noumaan/ssl-app-dev-a2923d5113c6
https://developer.android.com/studio/run/emulator-networking
This doc seems to focus on accessing a WebAPI, with a HttpClient, but I'm not sure that this scenario is correct for the WebView.
Noticed that from 'Chrome android', I got the following message: 'HTTP Error 400. The request hostname is invalid'. I got the following dialog:
Does my Blazor app (using a self-signed certificate) should use '10.0.2.2' instead of 'localhost' by example ?
If someone has any idea, really welcome :-)

In your applicationhost.config change in the bindingInformation the 'localhost' to '127.0.0.1'. That solved it for me.
See this answer where I found the solution.

Related

Android Emulator Sending local request to Azure Api results in 400 error [duplicate]

I have a Xamarin app and localhost API. I'm trying to connect to the API from the app, but it could not be found. I edited my applicationhost.config file and I tried localhost, 10.0.2.2 and my IP address in the call, but it was not working.
So, how do I configure simple API access? (The API itself is working well)
There is simple workaround to connect Emulator & Simulator to localhost API.
Download third party software NgRok for Windows at any specific location of your PC. It just contains one executable file called ngrok.exe (you not need to install it).
Now execute your service on your preferred browser. Than follow below steps.
Open your ngrok.exe file path in command prompt
Keep your service url (take from your browser) with prefix ngrok http --host-header=rewrite and hit enter.
Now go to your browser where you have executed your service open new tab and type localhost:4040 than press enter -->Go to status there you will find your public url which you can use in your mobile application.
Note: Url structure should be like this public const string BaseUri = "https://8c56892f.ngrok.io/"; followed by / in last. Try not to append unnecessary /.
Benifits
You can debug services from one Visual Studio to another.
Your url will be active until you close command prompt.
In browser or command prompt track your requests status like Ok, Not found etc.
For more information visit this https://www.c-sharpcorner.com/article/exposing-local-web-server-to-internet-using-ngrok/
Hope it help you.
That 10.0.2.2 address is used for the Google emulators. For the Microsoft emulator you want to use 169.254.80.80. If that doesn't work then it must mean you have the firewall on and it's blocking the traffic. That can be solved by adding a new incoming rule for the program or port.

chrome devTools port forwarding: how to use session cookie domain?

I am developing a web app using different sub domains and I'd like to go mobile first so I thought using the chrome devTools would be wonderful.
My /etc/hosts file looks like that on my local machine:
127.0.0.51 account.example.local
127.0.0.52 web.example.local
The user need to authenticate on account.example.local and get redirected to web.example.local.
The problem here is that I had to set session_cookie_domain = '.example.local', so they can share the same session. Therefore, when I am using chrome devTools with port forwarding to test on my android device, I can't pass the login page because the device use localhost:5000 and I can't set session_cookie_domain = 'localhost' of course due to cookies specifications.
Question : Did anybody manage to make this kind of settings to work? Or is it simply impossible today ?
Thanks !
You can map to custom local domains, although I'll warn you, it may take some trial and error.

400 bad request accessing ASP.NET mvc 4 application from android

I have a phonegapp App running on an Android emulator and I would like to access my ASP.NET MVC 4 server in order to make a log in request.
The problem is that my Emulator can't access my IIS application.
I tried this :
Use 10.0.2.2:port to refer my web site from the android navigator.
Use my local ip address (192.168.0.1:port)
Disabled my Firewall
Change bindings in my configuration file in IISExpress
Despite of that, i've got a "400 bad request error invalid username"...
Can anyone help me ?
I had the same problem. the port number in url isn't recognized by android emulator. so wehn I open visual studio project settings, I changed the IIS settings to have a virtual directory under the local host (for example "localhost/testPorject") instead of "localhost:PortNumer". this worked and the emulator recognized it.

Android - Worklight Hybrid application fails to connect to server in the emulator

I created a worklight application. I tried running that application on android emulator and i am getting the following error.
[http://localhost:8080/apps/services/api/MyFirstApp/android/query] Host is not responsive.
Try to manually access the URL through the android emulator browser to verify connectivity
So i tried to enter the same url in the emulator browser, i got the following text:
/*-secure-
{"WL-Authentication-Failure": {"wl_remoteDisableRealm":{"reason":"Login Failed}}} */
Can someone help me with this?
I am using Android OS version: 4.2.2, API level: 17
I haven't changed any application settings myself..
I am trying to go to a basic login page with the url "https://jazz.net/jazz/auth/j_security_check"
This is my adapter XML:
<wl:adapter xmlns:wl="worklight.com/integration"; xmlns:http="worklight.com/integration/http"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; name="RQMAdapter">
<displayName>RQMAdapter</displayName>
<description>HTTP type Worklight Adapter for jazz server</description>
<connectivity>
<connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
<protocol>https</protocol>
<domain>jazz.net</domain>
</connectionPolicy>
<loadConstraints maxConcurrentConnectionsPerNode="2" />
</connectivity>
<procedure name="loginToRQM" />
</wl:adapter>
This is my scenario:
I have a requirement where I need to read information about the projects from jazz.net for a user. They are all available on the jazz server within an XML file. I need to process it to a mobile device to make it available on hand held device... So, now I am told that i will have to download some of the files from the server, so i need to have file IO on my app. I learned that we need to write this part using a Cordova plug-in in Worklight.
please let me know what else is needed for the solution... thank you
Make sure to place the actual IP address of your server machine:
Change the value for worklightServerRootURL in the file
application-descriptor.xml (located at: yourProject\apps\yourApp)
Build and deploy
Right-click on the generated Android project >> Run As >> Run application
If you are using using Worklight 5.0.5, make sure to upgrade to 5.0.6, where a fix is available when only API Level 17 is installed.
As for going to that URL, you need to explain where exactly you're trying to go to it from, and how... you need to be more descriptive. It sounds to me like you haven't gone through the Getting Started material. Please do.
I had the same problem.
I have solved it by using the real IP address of the server in the WL app instead of using localhost:
When the emulator is running your app, press the Menu button.
Select to change your server URL
Remove "localhost" and use the IP address.
Accept and try again.
This fixed my problem.

WebView and localhost

I am getting google.com inside my WebView, but am not getting my localhost in that.
Can anybody help me?
Use 10.0.2.2 instead. This is a special ip address that the emulator routs to local host on your dev machine. assuming that emulator and the webserver are running on the same machine.
Make sure you are loading WebView with full Urls.
http://www.google.com
http://10.0.2.2/
and not:
www.google.com
google.com
All of these will work while using Browser, but you must supply a full valid url for WebView. Banged my head against the keyboard for an hour figuring that out.
If you're having connectivity issues in the emulator, try overriding the dns in the build path.
Window > Preferences > Android > Launch
Default emulator options: -dns-server 8.8.8.8
I have created a website and published it in my local computer using IIS, I tried to access this site in the android emulator using WebView:
http://localhost:83
with the same results as the OP.
I changed to:
http://10.0.2.2:83
and worked.
Try your actual ip [with the port number if required] instead of localhost.
Just type localhost name and port like this: http://127.0.0.1:80
If you're using a physical device to run, then,
make sure your website is running at 192.168.x.xxx:port
set the full url in your activity, such as,
mwebView.loadUrl("http://192.168.1.107:8000/");
add
android:usesCleartextTraffic="true"
in AndroidManifest.xml to fix 'cleartext not permitted' error caused for not using https
If in emulator, then,
host website in localhost (127.0.0.1:port)
for url
mwebView.loadUrl("http://127.0.0.1:3000");
previous 3rd step
(Make sure to remove the manifest permission before release. Or it would be a security concern for your app.)
I added the IP Address of the local server and it is working fine.
You can find you ip address by going to
COMMAND PROMPT
type> ipconfig
Now, add the ipv4 IP Address shown in your Webview URL. It will work fine.

Categories

Resources