diff --git a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java index 46eacbd472e2..6d596c092e14 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java +++ b/client-runtime/src/main/java/com/microsoft/rest/BaseUrlHandler.java @@ -7,13 +7,13 @@ package com.microsoft.rest; -import java.io.IOException; - import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; +import java.io.IOException; + /** * Handles dynamic replacements on base URL. The arguments must be in pairs * with the string in raw URL to replace as replacements[i] and the dynamic @@ -34,6 +34,7 @@ public Response intercept(Chain chain) throws IOException { for (int i = 0; i < replacements.length; i += 2) { baseUrl = baseUrl.replaceAll("(?i)\\Q" + replacements[i] + "\\E", replacements[i + 1]); } + baseUrl = removeRedundantProtocol(baseUrl); HttpUrl baseHttpUrl = HttpUrl.parse(baseUrl); request = request.newBuilder() .url(baseHttpUrl) @@ -42,4 +43,12 @@ public Response intercept(Chain chain) throws IOException { } return chain.proceed(request); } + + private String removeRedundantProtocol(String url) { + int last = url.lastIndexOf("://") - 1; + while (last >= 0 && Character.isLetter(url.charAt(last))) { + --last; + } + return url.substring(last + 1); + } }