Android每週一輪子:HttpURLConnection

Jensen95發表於2018-03-11

序言

接著上一篇的Volley,本篇原定計劃是OkHttp的,但是在分析道OKhttp底層時,對於IO的包裝等等特性,需要一個可參照的對比的例子,比如HttpURLConnection等,通過這種對比,才可以看的出其優勢。對於Volley,其實只是對於底層網路庫的封裝,真正的網路請求的發起還是通過HttpStack來執行,HttpStack在此之前可選的為HttpClient和HttpURLConnection。這裡針對HttpURLConnection展開進行分析。分析這樣的一個網路庫是如何實現的。本程式碼基於Android 4.3,4.4和其之後底層的實現採用了OkHttp。

基礎使用

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
} finally {
     urlConnection.disconnect();
}
複製程式碼

建立連線之後,獲得一個InputStream,我們就可以從中讀取連線建立後的返回資料。如果我們需要在請求中新增引數也可以通過獲取一個輸出流,在輸出流中寫入我們的請求資料。而其底層背後就是建立的一個Socket。

Socket模型

原始碼實現

首先是獲取HttpURLConnection

HttpURLConnection建立流程

首先呼叫了URL的openConnection方法。

public URLConnection openConnection() throws java.io.IOException {
      return handler.openConnection(this);
 }
複製程式碼

通過原始碼可以看出,對於連線的操作都是通過URLStreamHandler來進行的,對於URLStreamHandler的建立是在URL的建構函式之中。

public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException {
    ....
    if (streamHandler == null) {
        setupStreamHandler();
        if (streamHandler == null) {
            throw new MalformedURLException("Unknown protocol: " + protocol);
        }
    }
   try {
        streamHandler.parseURL(this, spec, schemeSpecificPartStart, spec.length());
    } catch (Exception e) {
        throw new MalformedURLException(e.toString());
    }
}
複製程式碼

StreamHandler

對於StreamHandler,有多個子類,分別可以用來進行http,https,ftp等協議流的處理。下面是HttpHandler的建立過程。

void setupStreamHandler() {
  //檢查是否有快取的處理相應協議的StreamHandler
    streamHandler = streamHandlers.get(protocol);
    if (streamHandler != null) {
        return;
    }

    //如果streamHandlerFactory不為空,通過其建立streamHandler,並將其快取下來
    if (streamHandlerFactory != null) {
        streamHandler = streamHandlerFactory.createURLStreamHandler(protocol);
        if (streamHandler != null) {
            streamHandlers.put(protocol, streamHandler);
            return;
        }
    }

    //從使用者提供的包中載入相應的StreamHandler,建立相應的例項,並加入到記憶體快取中,按照制定的路徑
    String packageList = System.getProperty("java.protocol.handler.pkgs");
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    if (packageList != null && contextClassLoader != null) {
        for (String packageName : packageList.split("\\|")) {
            String className = packageName + "." + protocol + ".Handler";
            try {
                Class<?> c = contextClassLoader.loadClass(className);
                streamHandler = (URLStreamHandler) c.newInstance();
                if (streamHandler != null) {
                    streamHandlers.put(protocol, streamHandler);
                }
                return;
            } catch (IllegalAccessException ignored) {
            } catch (InstantiationException ignored) {
            } catch (ClassNotFoundException ignored) {
            }
        }
    }

    // 如果使用者沒有提供,則會根據協議的要求,載入相應的Handler
    if (protocol.equals("file")) {
        streamHandler = new FileHandler();
    } else if (protocol.equals("ftp")) {
        streamHandler = new FtpHandler();
    } else if (protocol.equals("http")) {
        streamHandler = new HttpHandler();
    } else if (protocol.equals("https")) {
        streamHandler = new HttpsHandler();
    } else if (protocol.equals("jar")) {
        streamHandler = new JarHandler();
    }
    //將Handler加入到快取
    if (streamHandler != null) {
        streamHandlers.put(protocol, streamHandler);
    }
}
複製程式碼

HttpHandler建立過程

首先判斷是否已經有建立,從Handler列表中獲取,如果沒有判斷Handler工廠是否存在,如果不存在,載入本地的指定路徑,從中載入並建立相應的例項,最後如果本地路徑也沒有,則根據協議的型別,建立相應的協議Handler。

  • HttpHandler

這裡我們只針對Http協議來看,跟進下HttpHandler的原始碼,來了解一下其實現。Handler的連線建立,通過HttpURLConnectionImpl例項來進行。

protected URLConnection openConnection(URL u) throws IOException {
    return new HttpURLConnectionImpl(u, getDefaultPort());
}
複製程式碼
  • 獲取連線的輸出流
public final InputStream getInputStream() throws IOException {
    if (!doInput) {
        throw new ProtocolException("This protocol does not support input");
    }

    HttpEngine response = getResponse();

    if (getResponseCode() >= HTTP_BAD_REQUEST) {
        throw new FileNotFoundException(url.toString());
    }

    InputStream result = response.getResponseBody();
    if (result == null) {
        throw new IOException("No response body exists; responseCode=" + getResponseCode());
    }
    return result;
}
複製程式碼
  • HttpEngine的建立

呼叫getResponse獲得HttpEngine物件,從中獲取請求的內容。

private HttpEngine getResponse() throws IOException {
    //初始化HttpEngine
    initHttpEngine();
    //判斷HttpEngine如果有響應直接返回
    if (httpEngine.hasResponse()) {
        return httpEngine;
    }

    while (true) {
        try {
            httpEngine.sendRequest();
            httpEngine.readResponse();
        } catch (IOException e) {
            OutputStream requestBody = httpEngine.getRequestBody();
            if (httpEngine.hasRecycledConnection()
                    && (requestBody == null || requestBody instanceof RetryableOutputStream)) {
                httpEngine.release(false);
                httpEngine = newHttpEngine(method, rawRequestHeaders, null,
                        (RetryableOutputStream) requestBody);
                continue;
            }
            httpEngineFailure = e;
            throw e;
        }

        Retry retry = processResponseHeaders();
        if (retry == Retry.NONE) {
            httpEngine.automaticallyReleaseConnectionToPool();
            return httpEngine;
        }

        String retryMethod = method;
        OutputStream requestBody = httpEngine.getRequestBody();

        int responseCode = getResponseCode();
        if (responseCode == HTTP_MULT_CHOICE || responseCode == HTTP_MOVED_PERM
                || responseCode == HTTP_MOVED_TEMP || responseCode == HTTP_SEE_OTHER) {
            retryMethod = HttpEngine.GET;
            requestBody = null;
        }

        if (requestBody != null && !(requestBody instanceof RetryableOutputStream)) {
            throw new HttpRetryException("Cannot retry streamed HTTP body",
                    httpEngine.getResponseCode());
        }

        if (retry == Retry.DIFFERENT_CONNECTION) {
            httpEngine.automaticallyReleaseConnectionToPool();
        } else {
            httpEngine.markConnectionAsRecycled();
        }

        httpEngine.release(true);
        //建立HttpEngine
        httpEngine = newHttpEngine(retryMethod, rawRequestHeaders,
                httpEngine.getConnection(), (RetryableOutputStream) requestBody);
    }

}
複製程式碼

根據上述方法中的核心呼叫,逐步展開,首先是初始化HttpEngine,並建立其例項。

private void initHttpEngine() throws IOException {
    if (httpEngineFailure != null) {
        throw httpEngineFailure;
    } else if (httpEngine != null) {
        return;
    }

    connected = true;
    try {
        if (doOutput) {
            if (method == HttpEngine.GET) {
                // they are requesting a stream to write to. This implies a POST method
                method = HttpEngine.POST;
            } else if (method != HttpEngine.POST && method != HttpEngine.PUT) {
                // If the request method is neither POST nor PUT, then you're not writing
                throw new ProtocolException(method + " does not support writing");
            }
        }
        httpEngine = newHttpEngine(method, rawRequestHeaders, null, null);
    } catch (IOException e) {
        httpEngineFailure = e;
        throw e;
    }
}
複製程式碼
protected HttpEngine newHttpEngine(String method, RawHeaders requestHeaders,
        HttpConnection connection, RetryableOutputStream requestBody) throws IOException {
    return new HttpEngine(this, method, requestHeaders, connection, requestBody);
}
複製程式碼

在newHttpEngie中,new了一個HttpEngine的例項。

public HttpEngine(HttpURLConnectionImpl policy, String method, RawHeaders requestHeaders,
        HttpConnection connection, RetryableOutputStream requestBodyOut) throws IOException {
    this.policy = policy;
    this.method = method;
    this.connection = connection;
    this.requestBodyOut = requestBodyOut;

    try {
        uri = policy.getURL().toURILenient();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }

    this.requestHeaders = new RequestHeaders(uri, new RawHeaders(requestHeaders));
}
複製程式碼

在執行完了HttpEngine的初始化方法之後,呼叫了其sendRequest方法,首先會進行快取的判斷,最後會判斷其是否需要連線,如果需要,則會呼叫相應的連線方法:sendSocketRequest。

public final void sendRequest() throws IOException {
    if (responseSource != null) {
        return;
    }

    prepareRawRequestHeaders();
    initResponseSource();
    if (responseCache instanceof ExtendedResponseCache) {
        ((ExtendedResponseCache) responseCache).trackResponse(responseSource);
    }


    if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
        if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
            IoUtils.closeQuietly(cachedResponseBody);
        }
        this.responseSource = ResponseSource.CACHE;
        this.cacheResponse = GATEWAY_TIMEOUT_RESPONSE;
        RawHeaders rawResponseHeaders = RawHeaders.fromMultimap(cacheResponse.getHeaders());
        setResponse(new ResponseHeaders(uri, rawResponseHeaders), cacheResponse.getBody());
    }

    if (responseSource.requiresConnection()) {
        //放鬆socket建立請求
        sendSocketRequest();
    } else if (connection != null) {
        HttpConnectionPool.INSTANCE.recycle(connection);
        connection = null;
    }
}
複製程式碼

首先判斷connection是否為空,如果為空,呼叫connect方法,然後獲得該連線的OutputStream,InputStream。

private void sendSocketRequest() throws IOException {
    if (connection == null) {
        connect();
    }

    if (socketOut != null || requestOut != null || socketIn != null) {
        throw new IllegalStateException();
    }
    //建立socket後,返回其讀寫流
    socketOut = connection.getOutputStream();
    requestOut = socketOut;
    socketIn = connection.getInputStream();

    if (hasRequestBody()) {
        initRequestBodyOut();
    }
}
複製程式碼

實際連線過程呼叫

protected void connect() throws IOException {
    if (connection == null) {
        connection = openSocketConnection();
    }
}
複製程式碼

開啟Socket連線,這裡呼叫了HttpConnect的連線函式。

protected final HttpConnection openSocketConnection() throws IOException {
    HttpConnection result = HttpConnection.connect(uri, getSslSocketFactory(),
            policy.getProxy(), requiresTunnel(), policy.getConnectTimeout());
    Proxy proxy = result.getAddress().getProxy();
    if (proxy != null) {
        policy.setProxy(proxy);
    }
    result.setSoTimeout(policy.getReadTimeout());
    return result;
}
複製程式碼

對於具體的連線任務交給了HttpConnection來處理,呼叫其連線方法。會從連線池中獲取相應的連線,呼叫其get方法。

public static HttpConnection connect(URI uri, SSLSocketFactory sslSocketFactory,
        Proxy proxy, boolean requiresTunnel, int connectTimeout) throws IOException {

    if (proxy != null) {
        Address address = (proxy.type() == Proxy.Type.DIRECT)
                ? new Address(uri, sslSocketFactory)
                : new Address(uri, sslSocketFactory, proxy, requiresTunnel);
        return HttpConnectionPool.INSTANCE.get(address, connectTimeout);
    }

    /*
     * Try connecting to each of the proxies provided by the ProxySelector
     * until a connection succeeds.
     */
    ProxySelector selector = ProxySelector.getDefault();
    List<Proxy> proxyList = selector.select(uri);
    if (proxyList != null) {
        for (Proxy selectedProxy : proxyList) {
            if (selectedProxy.type() == Proxy.Type.DIRECT) {
                continue;
            }
            try {
                Address address = new Address(uri, sslSocketFactory,
                        selectedProxy, requiresTunnel);
                return HttpConnectionPool.INSTANCE.get(address, connectTimeout);
            } catch (IOException e) {
                // failed to connect, tell it to the selector
                selector.connectFailed(uri, selectedProxy.address(), e);
            }
        }
    }

    /*
     * Try a direct connection. If this fails, this method will throw.
     */
    return HttpConnectionPool.INSTANCE.get(new Address(uri, sslSocketFactory), connectTimeout);
}
複製程式碼

根據傳遞的請求,從HttpConnectionPool中獲取HttpConnection連線。當其中不存在該連線的時候,重新建立一個例項,然後返回。

public HttpConnection get(HttpConnection.Address address, int connectTimeout)
        throws IOException {
    // First try to reuse an existing HTTP connection.
    synchronized (connectionPool) {
        List<HttpConnection> connections = connectionPool.get(address);
        while (connections != null) {
            HttpConnection connection = connections.remove(connections.size() - 1);
            if (connections.isEmpty()) {
                connectionPool.remove(address);
                connections = null;
            }
            if (connection.isEligibleForRecycling()) {
                // Since Socket is recycled, re-tag before using
                Socket socket = connection.getSocket();
                SocketTagger.get().tag(socket);
                return connection;
            }
        }
    }

    //當我們無法找到一個可用的連線,這個時候,我們需要重新建立一個新的連線
    return address.connect(connectTimeout);
}
複製程式碼

Address 為HttpConnection的一個內部類。

public HttpConnection connect(int connectTimeout) throws IOException {
    return new HttpConnection(this, connectTimeout);
}
複製程式碼

此時會再建立一個新的連線 ,在HttpConnection的建構函式之中是真正的socket連線建立的地方。

private HttpConnection(Address config, int connectTimeout) throws IOException {
    this.address = config;
    Socket socketCandidate = null;
    InetAddress[] addresses = InetAddress.getAllByName(config.socketHost);
    for (int i = 0; i < addresses.length; i++) {
        socketCandidate = (config.proxy != null && config.proxy.type() != Proxy.Type.HTTP)
                ? new Socket(config.proxy)
                : new Socket();
        try {
            socketCandidate.connect(
                    new InetSocketAddress(addresses[i], config.socketPort), connectTimeout);
            break;
        } catch (IOException e) {
            if (i == addresses.length - 1) {
                throw e;
            }
        }
    }

    this.socket = socketCandidate;
}
複製程式碼

請求體的寫入,獲得Socket的寫入流,然後將我們的請求資料寫入

private void writeRequestHeaders(int contentLength) throws IOException {
    if (sentRequestMillis != -1) {
        throw new IllegalStateException();
    }

    RawHeaders headersToSend = getNetworkRequestHeaders();
    byte[] bytes = headersToSend.toHeaderString().getBytes(Charsets.ISO_8859_1);

    if (contentLength != -1 && bytes.length + contentLength <= MAX_REQUEST_BUFFER_LENGTH) {
        requestOut = new BufferedOutputStream(socketOut, bytes.length + contentLength);
    }

    sentRequestMillis = System.currentTimeMillis();
    requestOut.write(bytes);
}
複製程式碼

連線的建立過程

小結

對於HttpURLConnection庫,是一個相對比較簡單的網路庫,最開始通過根據設定的URL資訊,建立一個Socket連線,然後獲得Socket連線後得到Socket的InputStream和OutputStream,然後通過其獲取資料和寫入資料,其內部提供的功能比較少,僅限於幫助我們做一些簡單的http的包裝,核心類是HttpConnection,HttpEngine兩個類。

相關文章