简体   繁体   English

Android检查WebService是否在端口上可用

[英]Android Check if WebService is available on port

I need to check if a service is running on a specific IP:PORT/service/ and if its not then fallback to another one. 我需要检查某个服务是否正在特定的IP:PORT / service /上运行,如果没有,则回退到另一服务。 I am doing this on a single place in the app. 我正在应用程序中的单个位置上执行此操作。

Is there a way to just PING it if its alive instead of trying to send a valid request to it and catch a possible IO error? 有没有一种方法可以对它执行PING操作,而不是尝试向其发送有效请求并捕获可能的IO错误?

Refer accepted answer this question for web service availability check, if webservice is not there (else condition) then try fallback connection 推荐接受此问题的答案以进行Web服务可用性检查,如果Web服务不存在(其他情况),则尝试回退连接

webservice availability check in android android中的web服务可用性检查

The way i solved this is: 我解决这个问题的方法是:

We implemented a Ping method on the backend at /service/ping which responded with status HTTP_OK (200) . 我们在/service/ping的后端上实现了一种Ping方法,该方法以状态HTTP_OK (200)响应。 i walk through each IP address i need to test with a very short connection timeout set and see if i get to one where i receive a successful connection (status returned is 200). 我遍历了每个IP地址,并设置了非常短的连接超时来进行测试,看是否能到达一个接收成功连接的状态(返回的状态为200)。

I had to go through all the IP's on a local wifi network and find where my local network service was hosted. 我必须遍历本地wifi网络上的所有IP,然后找到本地网络服务的托管位置。 Since the devices are connected to the same wifi network i have setup a connection timeout of 200ms which was more than enough to make sure the check was fast. 由于设备连接到相同的wifi网络,所以我设置了200ms的连接超时,这足以确保检查很快。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM