.NetCore PostAsync和GetAsync不适用于Docker

我在.NetCore 1.1中创build了一个需要调用外部服务的WebAPI。 当我运行它与dotnet run它完美的作品,但是当我使用Docker我得到一个

发生一个或多个错误。 (发送请求时发生错误。)

这是我的Post方法:

 public static string Post(string url, string jsonObject, string serviceName) { // opening a connection to the microservice using (HttpClient client = new HttpClient()) using (HttpResponseMessage response = client.PostAsync(url, new StringContent(jsonObject, Encoding.UTF8, "application/json")).Result) { LogWrapper.SafeInfo(LoggingEvents.SERVICECALLER_GETSERVICESRESPONSE, "Let's test if we get an OK response from " + serviceName); // we'll throw an exception in case status code is different from 200 response.EnsureSuccessStatusCode(); LogWrapper.SafeInfo(LoggingEvents.SERVICECALLER_GETSERVICESRESPONSE, "We got an OK response from " + serviceName); using (HttpContent responseContent = response.Content) { return responseContent.ReadAsStringAsync().Result; } } } 

这个错误是在client.PostAsync行中client.PostAsync 。 在GET方法中发生同样的错误。

我已经检查了URL和参数。

更让人感兴趣的是,它只发生在我的应用程序在Docker容器中运行时。 当我运行没有Docker的应用程序时,它工作得很好。

谢谢你的帮助。

对不起,由于缺乏细节,但经过一些阅读和谷歌search,我终于发现了这个问题。

我有两个容器在本地主机上运行,所以我正在请求从容器A到容器B的服务。

我使用了http://127.0.0.1:356/api/LogIn地址,但问题是在这种情况下localhost并不指向我的机器,而是指向容器A本身。 这就是为什么我可以使它与dotnet run但与Docker无关。

事实上, 这个答案让我看到了我的错误。