从Xamarin.Forms移动应用程序(以及同一网络上的其他计算机)连接到一个RESTful API[英] Connecting to a RESTful API from Xamarin.Forms mobile application (and other computers on the same network)

本文是小编为大家收集整理的关于从Xamarin.Forms移动应用程序(以及同一网络上的其他计算机)连接到一个RESTful API的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试将我的Xamarin.Forms移动应用程序连接到我本地在计算机上托管的RESTFUL API.我将Visual Studio用于Xamarin.Forms移动应用程序和API,并且对于API,我使用的是ASP.NET Core(Visual Studio中的特定模板是ASP.NET Core Web应用程序(.NET框架(.NET Framework) ).
当我使用同一台计算机上的Localhost地址连接到浏览器时,它可以正常运行(在我的情况下是Localhost:59475/api/是URL),然后将我的移动应用程序连接到我的移动应用程序之前API,我首先试图从同一网络上的其他计算机连接到API,但我没有设法使其运行.到目前为止,我尝试的是以下内容 - 运行CMD命令netsh http add urlacl url=http://*:59475/ user=everyone(以及其他类似命令以及我认为可以工作的其他端口),然后我添加了一个入站防火墙规则,以允许该端口上的所有连接.然后,我尝试通过使用运行API的计算机的IP替换网络上其他计算机的API,并在此操作时收到HTTP 503错误.从视觉工作室中运行API时,从意识到的是,它显示出

的错误

无法连接到Web Server'iis Express'

"取消" netsh http命令(netsh http删除urllacl ...)使它再次工作,就像刚开始一样(再次,只能使用我的浏览器从同一计算机连接到它).我不知道如何使它起作用,并希望获得帮助! :)

推荐答案

尝试以下内容:

  1. 明确添加外部IP netsh http add urlacl url=http://192.168.0.6:60985/ user=everyone

  2. 在API解决方案文件夹的根中打开'.vs/config/applicationhost.config'文件(默认情况下是隐藏的)

  3. 在"绑定"部分中,向外部地址添加了新的绑定<binding protocol="http" bindingInformation="*:60985:192.168.0.6" />,请注意您要在哪个站点添加绑定,因为可能有多个.

注意:示例端口60985您的可能是不同的检查API 示例IP:192.168.0.6,更改为您的实际外部IP地址

您可能需要禁用防火墙进行测试,然后确认它有效,启用并添加异常规则

其他推荐答案

我也有同样的问题. 我用神奇的传送带扩展解决了. html?utm_source =传送带&utm_medium = extension_moreinfo&utm_campaign =传送带

遵循其网站上的指南. 在NetCore中,禁用SSL首先(Project Property =>调试)

本文地址:https://www.itbaoku.cn/post/1937785.html

问题描述

I am trying to connect my Xamarin.Forms mobile application to a RESTful API I'm hosting locally on my computer. I'm using Visual Studio for both the Xamarin.Forms mobile application and for the API, and for the API, I'm using ASP.NET Core (the specific template in Visual Studio is ASP.NET Core Web Application (.NET Framework) ).
It worked fine when I connected to it from my browser using a localhost address on the same computer I ran the API from (in my case, localhost:59475/api/ was the URL) and before connecting my mobile application to the API, I first tried to connect to the API from other computers on the same network, and I didn't manage to make it work so far. What I tried so far is the following - run the cmd command netsh http add urlacl url=http://*:59475/ user=everyone (along with other similar commands with other ports I thought may work), then I added an inbound firewall rule to allow all connections on that port. I then tried to connect to the API from other computers on the network by replacing "localhost" in the address with the IP of the computer running the API, and I received an HTTP 503 error when I did. Evantually what happend is when I tried to run the API from Visual Studio it showed an error of

Unable to connect to web server 'IIS Express'

"Undoing" the netsh http commands (netsh http delete urlacl...) made it work again as it did at first (again, only able to connect to it from the same computer using my browser). I don't know how to make it work and would like to receive help please! :)

推荐答案

Try the following:

  1. add your external IP explicitly netsh http add urlacl url=http://192.168.0.6:60985/ user=everyone

  2. In the root of the API solution folder open the '.vs/config/applicationhost.config' file (.vs is hidden by default)

  3. in the bindings section add a new binding to the external address <binding protocol="http" bindingInformation="*:60985:192.168.0.6" />, mind to which site you are adding the bindings as there could be multiple.

Notes: example port 60985 yours could be different check the api example IP: 192.168.0.6, change to your actual external IP address

You might want to disable the firewall for testing then when you confirm it works, enable it and add an exception rule

其他推荐答案

I had the same problem. I solved with the magical Conveyor extension. https://keyoti.com/products/conveyor/index.html?utm_source=conveyor&utm_medium=extension_moreinfo&utm_campaign=conveyor

Follow the guide on their website. In netcore, disable SSL first (project property => debug)