【问题标题】:Xamarin.Forms Android : access localhost (10.0.2.2) from WebViewXamarin.Forms Android:从 WebView 访问 localhost (10.0.2.2)
【发布时间】:2020-11-15 11:20:13
【问题描述】:

我有一个 Xamarin.Forms 移动应用程序,这个应用程序包含一个 WebView。 这个 WebView URL 应该是我的本地开发机器,因为我在那里运行了一个 Web 应用程序。

所以,在运行 Android 模拟器时,我尝试使用环回地址 https://10.0.2.2:44390 ...但 WebView 仍然是白色的!

我已阅读以下文档,但无济于事:

https://docs.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

该文档似乎侧重于使用 HttpClient 访问 WebAPI,但我不确定这种情况对于 WebView 是否正确。

注意到从“Chrome android”中,我收到以下消息:“HTTP 错误 400。请求主机名无效”。我得到了以下对话框:

例如,我的 Blazor 应用程序(使用自签名证书)是否应该使用“10.0.2.2”而不是“localhost”?

如果有人有任何想法,非常欢迎:-)

【问题讨论】:

  • 使用你服务器的实际IP
  • 谢谢,我已经试过了,运气不好,这样不行!
  • 如果您的服务器的 IP 不起作用,那么您的网络设置有问题。你可以从模拟器上的 Chrome 连接吗?
  • 我主要使用Android loopback,10.0.2.2,它应该指向我的'windows localhost'。我将编辑我的问题以添加一些细节!

标签: android xamarin.forms webview xamarin.android loopback-address


【解决方案1】:

在您的 applicationhost.config 中将 bindingInformation 中的 'localhost' 更改为 '127.0.0.1'。这为我解决了它。 看到这个answer 我找到了解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2016-08-13
    • 2015-11-22
    • 2011-08-14
    • 2015-10-12
    • 2018-08-21
    相关资源
    最近更新 更多