使用HttpClient连接到API时出现错误

时间:2019-08-22 13:24:41

标签: c# xamarin.forms asp.net-core-webapi

这是HttpClientWrapper上的问题,试图从设备/仿真器连接到API端点吗?

string result = _failedResult;
        try
        {
            using (var httpClient = new HttpClient())
            {
                //Set the headers
                httpClient.DefaultRequestHeaders.Accept.Clear();
                using (HttpResponseMessage responce = httpClient.GetAsync(uri).Result)
                {
                    if (responce.IsSuccessStatusCode)
                    {
                        result = responce.Content.ReadAsStringAsync().Result;
                    }
                    else
                    {
                        var reasonPhrase = responce.ReasonPhrase;
                        result = responce.Content.ReadAsStringAsync().Result;
                        var errormessage = string.Format("Error:{0} {1} for uri :{2} ", reasonPhrase, result, uri.ToString());
                    }
                }
            }
        }
        catch(Exception ex)
        {
            var t=ex.InnerException;
            result = ex.Message;
        }

        return result;

期望我需要从服务器获取电话号码列表作为JSON格式。

相反,我收到以下错误:

  

System.Net.WebException:错误:ConnectFailure(连接被拒绝)->   System.Net.Sockets.SocketException:连接被拒绝

1 个答案:

答案 0 :(得分:0)

如果您使用的是local Service = game:GetService("TweenService") local anticlimb = workspace.Gate.anticlimb local Info = TweenInfo.new(2) script.Parent.ClickDetector.MouseClick:Connect(function() script.Parent.CanCollide = false local Change = {Position = Vector3.new(175.312, 8.559, 230.363)} end) function TweenModel(model, position, tweenInfo) local val = Instance.new("CFrameValue") val.Value = model:GetPrimaryPartCFrame() val:GetPropertyChangedSignal("Value"):Connect(function() model:SetPrimaryPartCFrame(val.Value) end) local tween = TweenService:Create(val, tweenInfo, {Value = CFrame.new(175.312, 8.559, 230.363)}) tween:Play() return tween end``` No error outputs what so ever from running this on a test server or in game. :( ,那么询问localhost Web服务将不起作用,因为您正在查看仿真器的Android Emulator

您可以按以下步骤解决此问题:

localhost的魔幻地址Android Emulator指向主机上的http://10.0.2.2:your_port。看一下here。因为它指向IP,而不是localhost,所以您需要进入项目解决方案,位于.vs文件夹-> config-> applicationhost.config中,并将此127.0.0.1:your_port更改为,其中44388是您的端口。重新启动IIS Express,一切顺利。

因此,请使用:“ http://10.0.2.2:44388/api/Common/getPhonenumbers

相关问题