在使用Xamarin.Forms(PCL)中使用HTTPCLIENT方法之前,请先检查Web服务的可用性



我在使用HttpClient方法之前正在测试我的Web服务(本地)可用性

我正在使用当我将URL" Google.com"放置时,下面的代码正常工作,当我给我的Web服务URL时,我想在我的URL中包含端口号,该如何实现

  if (CrossConnectivity.Current.IsConnected)
            {
                bool isRemoteReachable = await CrossConnectivity.Current.IsReachable("192.168.1.107",3);
                if (!isRemoteReachable)
                {
                    await DisplayAlert("Server not available", "Please try again later", "OK");
                }
            }

根据官方连接插件文档,为了设置端口,您应该以下一个方式调用该方法:

/// <summary>
/// Tests if a remote host name is reachable (no http:// or www.)
/// </summary>
/// <param name="host">Host name can be a remote IP or URL of website</param>
/// <param name="port">Port to attempt to check is reachable.</param>
/// <param name="msTimeout">Timeout in milliseconds.</param>
/// <returns></returns>
Task<bool> IsRemoteReachable(string host, int port = 80, int msTimeout = 5000);

或内部主机:

/// <summary>
/// Tests if a host name is pingable
/// </summary>
/// <param name="host">The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address (127.0.0.1)</param>
/// <param name="msTimeout">Timeout in milliseconds</param>
/// <returns></returns>
Task<bool> IsReachable(string host, int msTimeout = 5000);

我认为应该翻译成:

IsReachable("192.168.1.107:8733", 30 * 1000);

请注意Mstimeout您在那里的价值为3ms,否则该方法可能会立即返回,而结果可能会立即返回。

编辑:
经过短暂的聊天后,事实证明这是一个特定的问题。我建议您自己编写ping方法或将错误提交连接插件github页面。

最新更新