{"record":{"id":"58870ec1bf2de22d","repo":"didi/DoKit","slug":"unexpected-protocol-protocol","errorCode":null,"errorMessage":"Unexpected protocol: ${protocol}","messagePattern":"Unexpected protocol: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java","lineNumber":158,"sourceCode":"     */\n    @Override\n    public ObsoleteUrlFactory clone() {\n        return new ObsoleteUrlFactory(client);\n    }\n\n    public HttpURLConnection open(URL url) {\n        return open(url, client.proxy());\n    }\n\n    HttpURLConnection open(URL url, @Nullable Proxy proxy) {\n        String protocol = url.getProtocol();\n        OkHttpClient copy = client.newBuilder()\n                .proxy(proxy)\n                .build();\n\n        if (protocol.equals(\"http\")) return new OkHttpURLConnection(url, copy);\n        if (protocol.equals(\"https\")) return new OkHttpsURLConnection(url, copy);\n        throw new IllegalArgumentException(\"Unexpected protocol: \" + protocol);\n    }\n\n    /**\n     * Creates a URLStreamHandler as a {@link URL#setURLStreamHandlerFactory}.\n     *\n     * <p>This code configures OkHttp to handle all HTTP and HTTPS connections\n     * created with {@link URL#openConnection()}: <pre>   {@code\n     *\n     *   OkHttpClient okHttpClient = new OkHttpClient();\n     *   URL.setURLStreamHandlerFactory(new ObsoleteUrlFactory(okHttpClient));\n     * }</pre>\n     */\n    @Override\n    public URLStreamHandler createURLStreamHandler(final String protocol) {\n        if (!protocol.equals(\"http\") && !protocol.equals(\"https\")) return null;\n\n        return new URLStreamHandler() {\n            @Override","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L140-L176","documentation":"ObsoleteUrlFactory.open(URL, Proxy) inspects url.getProtocol() and only knows how to map \"http\" and \"https\" onto OkHttp connections; any other scheme falls through to IllegalArgumentException(\"Unexpected protocol: \" + protocol). This factory is DoKit's replacement for okhttp3.JavaNetUrlFactory so that URL.openConnection() traffic is routed through OkHttp for capture.","triggerScenarios":"Calling url.openConnection() (or open()) on a URL with a scheme like file:, ftp:, jar:, or a custom scheme while URL.setURLStreamHandlerFactory has installed this factory globally.","commonSituations":"DoKit installs the factory app-wide; later code opens a file: or jar: URL (very common for classpath resources, WebView assets, or local file access) and crashes because the factory intercepts every protocol.","solutions":["Avoid URL.openConnection() for non-http(s) schemes: use new FileInputStream(path) for files, or ClassLoader.getResourceAsStream() for classpath resources.","Normalize the URL to http/https before openConnection() when the resource is actually served over HTTP.","If you control the factory install, only install it for http/https or unset DoKit's URL-connection interception when non-HTTP protocols are needed."],"exampleFix":"// before\nInputStream in = new URL(\"file:///data/local/tmp/a.txt\").openConnection().getInputStream();\n\n// after\nInputStream in = new FileInputStream(\"/data/local/tmp/a.txt\");","handlingStrategy":"validation","validationCode":"String p = url.getProtocol();\nif (!(\"http\".equals(p) || \"https\".equals(p))) {\n  throw new UnsupportedOperationException(\"unsupported scheme: \" + p);\n}","typeGuard":"static boolean isHttpUrl(URL u) {\n  String p = u.getProtocol();\n  return \"http\".equals(p) || \"https\".equals(p);\n}","tryCatchPattern":"try { conn = factory.open(url); } catch (IllegalArgumentException e) { if (!isHttpUrl(url)) useNativeHandler(url); else throw e; }","preventionTips":["Route file: URLs to FileInputStream/ContentResolver, not openConnection().","If a custom URLStreamHandlerFactory is installed globally, remember it intercepts every scheme."],"tags":["network","url","okhttp","protocol"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}