Skip to content

Custom HTTP Clients

Alan Zimmer edited this page Feb 27, 2020 · 5 revisions

Introduction

Azure SDKs for Java offers the capability to plug-in your own custom networking layer to allow for handling of specialized scenarios or when you don't want to use Netty or OkHttp. Providing a custom HttpClient requires a few interfaces/classes to be implemented along with registering your implementation with Java's service provider interface.

Implementation Overview

Interfaces and Abstract Classes

The following interfaces and abstract classes will need to be implemented in your custom HTTP client.

  • HttpClient handles sending request and receiving responses.
  • HttpResponse contains response information and APIs to interact with it.
  • HttpClientProvider handles creating instances of the HttpClient agnostic to the underlying implementation.

Target Version of Azure Core

A specific version of Azure Core will need to be targeted when implementing a custom HTTP client. This version should be based on which version of Azure Core is being used by the other Azure SDK client libraries your application is depending on, for example if you have a dependency on Azure Storage Blobs that uses Azure Core 1.2.0 you'll want your custom HTTP client to use that as it's dependency.

Service Provider Interface (SPI)

Once the custom HTTP client has been implemented it needs to be registered with Java's service provider interface. In the module that contains the custom HTTP client the resource folder will need the following.

src
|
+--main
   |
   +--java
   +--resources
      |
      +--META-INF.services
         |
         +--com.azure.core.http.HttpClientProvider

The only contents of com.azure.core.http.HttpClientProvider file should be the fully qualified name of the class that implements HttpClientProvider. For example if this class is custom.http.MyHttpClientProvider the file would only contain custom.http.MyHttpClientProvider.

Example

This is a high-level example of a custom HTTP client implementation built on top of org.apache.httpcomponents.httpclient and using Azure Core 1.2.0. This example hasn't be verified for correctness or scalability but serves as a general example of how to roll your own HTTP client.

ApacheHttpClient

ApacheHttpResponse

ApachaeHttpClientProvider

com.azure.core.http.HttpClientProvider

Clone this wiki locally