简体   繁体   中英

Remote server availability check using OkHttp java

I am looking for some examples on how to check server availability over Http/2 in Java using OkHttp3. Couldn't find any examples.

I am looking for sources which provide information on

  • Certificates
  • Necessity of Credentials
  • Sample code, if available.

Are there any better alternatives to OkHttp3?

Any inputs in this regard are highly appreciated.

To check server availability, you need to first establish a connection to it.

This connection should also contain a valid request, so that you can check the answer you receive from the server, and make sure your request reached it and did not get blocked or redirected along the way.

To know what kind of request would be valid, and whether it requires credentials or not, you need to know exactly what the specific server you are testing expects.

It is up to the server to decide if you need any credentials, and overall, what you need to send to it to get a good response.

okhttp is considered a good library for communicating with http servers, but you may also want to check the validity of the internet connection on the machine doing the testing, to make sure that if a connection fails it is due to the problem on the server end and not the client end.

okhttp does not have specific utilities to do that, however, depending on the platform you are using, there are two possible methods:

  1. Use platform specific API (such as NetworkManager on Android) to check that the device has internet connection.

  2. Use okhttp to make a request to a server with very high availability, such as google.com, to see if you can reach it.

Here is a link with sample code for making an HTTP request with okhttp: https://www.vogella.com/tutorials/JavaLibrary-OkHttp/article.html

They even show one possible way of providing credentials, but again - it depends on the server you are trying to test!

A typical approach is to execute a request for https://host.com/robots.txt , maybe even a head request.

Say

fun main() {
  val client = OkHttpClient.Builder()
    .eventListener(object : EventListener() {
      override fun connectStart(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy) {
        println("connectStart $inetSocketAddress $proxy")
      }

      override fun secureConnectStart(call: Call) {
        println("secureConnectStart")
      }

      override fun secureConnectEnd(call: Call, handshake: Handshake?) {
        println("secureConnectEnd ${handshake?.cipherSuite}")
        handshake?.peerCertificates?.forEach {
          println((it as X509Certificate).subjectDN)
        }
      }

      override fun connectEnd(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy, protocol: Protocol?) {
        println("connectEnd")
      }

      override fun connectFailed(
        call: Call,
        inetSocketAddress: InetSocketAddress,
        proxy: Proxy,
        protocol: Protocol?,
        ioe: IOException
      ) {
        println("connectFailed $ioe")
      }
    })
    .build()

    val request = Request.Builder()
      .url("https://github.com/robots.txt")
      .head()
      .build()

    client.newCall(request).execute().use { response ->
      if (!response.isSuccessful) throw IOException("Unexpected code $response")
    }
}

Which outputs

connectStart github.com/140.82.121.3:443 DIRECT
secureConnectStart
secureConnectEnd TLS_AES_128_GCM_SHA256
CN=github.com, O="GitHub, Inc.", L=San Francisco, ST=California, C=US
CN=DigiCert High Assurance TLS Hybrid ECC SHA256 2020 CA1, O="DigiCert, Inc.", C=US
CN=DigiCert High Assurance EV Root CA, OU=www.digicert.com, O=DigiCert Inc, C=US
connectEnd

Things to be aware of, there can be multiple routes, if the first fails another may be tried.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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