简体   繁体   English

在 .NET Core 中,如何向在本地服务器上运行的 API 发出 HTTP 请求(IIS Express 使用 VS 2019)?

[英]In .NET Core, how do you make HTTP request to an API running on local server (IIS Express using VS 2019)?

So, I have an API running in Visual Studio 2019, and I can make a request by typing in a url in the browser, like所以,我有一个在 Visual Studio 2019 中运行的 API,我可以通过在浏览器中输入 url 来发出请求,比如

https://192.168.1.192:44327/api/pickslips

But when I try to make such a call from code, like for instance:但是当我尝试从代码中进行这样的调用时,例如:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var responseMessage = await httpClient.GetAsync(@"https://192.168.1.192:44327/api/pickslips");

var jsonResult = await responseMessage.Content.ReadAsStringAsync();
var slips = JsonConvert.DeserializeObject<IEnumerable<PickSlip>>(jsonResult);
return slips.ToList();

The application stops and exits at await GetAsync, without an error, and doesn't get to anything beyond it.应用程序在 await GetAsync 处停止并退出,没有出现错误,并且不会执行任何操作。 However, the code works when I try to make a call to other APIs like some of the test ones you can find on the internet.但是,当我尝试调用其他 API(例如您可以在 Internet 上找到的一些测试 API)时,该代码可以正常工作。

By the way, one of the reasons why it's not running on localhost is because I need it for Xamarin.顺便说一句,它不在 localhost 上运行的原因之一是因为我需要它用于 Xamarin。

So how can I make this work?那么我怎样才能使这项工作呢?

The application is unable to reach the IP address specific, throw an HTTP error, and the app crashes.应用程序无法访问特定的 IP 地址,抛出 HTTP 错误,应用程序崩溃。 To troubleshoot that you want to start with try/catch and read the error details.要解决您想从 try/catch 开始并阅读错误详细信息的问题。 Most likely you are hitting one of two issues:您很可能遇到以下两个问题之一:

  1. Your device/sim is unable to reach the given IP Address您的设备/sim 无法访问给定的 IP 地址
  2. You are trying to access your local HTTP server which doesn't allow remote connections您正在尝试访问不允许远程连接的本地 HTTP 服务器

In the first case, you want to verify that your device on the same network and can reach the IP address specified, in the case of sim you want to make it can access the network interface.在第一种情况下,你要验证你的设备在同一个网络上并且可以到达指定的IP地址,在sim的情况下你想让它可以访问网络接口。 Similar issues were answered here (for Android). 此处回答了类似的问题(适用于 Android)。

In the second case, you want to make sure your local HTTP server configured to allow remote connections.在第二种情况下,您要确保您的本地 HTTP 服务器配置为允许远程连接。 The fact you can access it from a browser is great, try to access it from a browser on another computer in the same network.您可以从浏览器访问它的事实很棒,请尝试从同一网络中另一台计算机上的浏览器访问它。 A similar issue was answered here . 这里回答了一个类似的问题。

If it's an emulator I don't think every emulator accesses the host through it's router's IP address, ie you should check to see for the emulator in question if it is in a sort of a VPN with your host machine and accesses it through an IP that's in a different class (usually 10.xxx if I remember) but it shouldn't work from the browser (on an emulator or device in question) so if that's the case, could you provide more info like is it an android or iOS, are you certainly accessing it through httpS and not http as you need to enable http on those recent mobile OS versions though the app's manifest?如果它是一个模拟器,我不认为每个模拟器都通过它的路由器的 IP 地址访问主机,即你应该检查有问题的模拟器是否与你的主机处于某种 VPN 中并通过 IP 访问它那是在不同的 class 中(如果我记得的话,通常是 10.xxx),但它不应该在浏览器中工作(在有问题的模拟器或设备上)所以如果是这种情况,你能否提供更多信息,比如它是 android 还是 Z1BDF11ZCBDF60590820CDB4EBF8 ,您肯定是通过 httpS 而不是 http 访问它,因为您需要通过应用程序的清单在那些最近的移动操作系统版本上启用 http?

Also if remote requests ie from other devices or emulator even are being blocked and just working locally then for a quick check try disabling your firewall, make sure it's not the problem, and enable it back on.此外,如果来自其他设备或模拟器的远程请求甚至被阻止并且只是在本地工作,那么为了快速检查,请尝试禁用您的防火墙,确保它不是问题,然后重新启用它。

Hope something helps.希望有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IIS 请求计数限制与 IIS Express - Web ZDB974238714CA8DE634A7CE1D083A - IIS Request Count Limitation vs IIS Express - Web API Core 在 ASP.NET Core 中,如何检查请求是否是本地的? - In ASP.NET Core how do you check if request is local? VS 2019中IIS Express调试器的.Net Core MVC appsettings.json文件在哪里 - Where is the .Net Core MVC appsettings.json file for the IIS Express debugger in VS 2019 ASP.NET web 应用程序 session 本地 IIS Express 上的超时问题 Windows Server 2019? - ASP.NET web application session timeout issue on local IIS Express on Windows Server 2019? 在 IIS 上启动 .NET Core Web API 时出错:“应用程序正在 IIS 进程内运行,但未配置为使用 IIS 服务器” - Error on starting .NET Core Web API on IIS: "Application is running inside IIS process but is not configured to use IIS server" .NET Core、VS2019 和 Selenium:IE 驱动程序不起作用(WebDriverException - HTTP 请求超时) - .NET Core, VS2019, and Selenium: IE Driver Doesn't Work (WebDriverException - HTTP request timeout) 应用程序池崩溃:IIS Express与本地/服务器IIS - App pool crash : IIS Express vs Local/Server IIS 在VS2019中使用.Net Core格式的项目文件时,如何自动增加程序包版本修订号 - How do I autoincrement the Package Version revision number when using a .Net Core format project file in VS2019 asp .net core api 发布在 iis 原因(本地主机当前无法处理此请求。Z293C9ZEA246FF9985866F) - asp .net core api published on iis cause (localhost is currently unable to handle this request. HTTP ERROR 500) ASP.NET 项目在 VS 运行时在本地运行,但不在本地计算机或服务器上的 IIS 上运行 - ASP.NET project works locally on build when VS is running but not on IIS on my local machine or my server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM