Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android網絡之HttpUrlConnection和Socket關系解析

Android網絡之HttpUrlConnection和Socket關系解析

編輯:關於Android編程

多年以前Android的網絡請求只有Apache開源的HttpClient和JDK的HttpUrlConnection,近幾年隨著OkHttp的流行Android在高版本的SDK中加入了OkHttp。但在Android官方文檔中推薦使用HttpUrlConnection並且其會一直被維護,所以在學習Android網絡相關的知識時我們隊HttpUrlConnection要有足夠的了解。。。。

前幾天因為時間的關系只畫了圖 HttpUrlConnection和Socket的關系圖 ,本來說好的第二天續寫,結果一直拖到了周末晚上。幸好時間還來的及,趁這短時間影響深刻,將自己解析代碼過程記錄下來。(PS:解析的過程有什麼地方不明白的可以看看 HttpUrlConnection和Socket的關系圖 圖中講出的過程和這次代碼分析的過程是一樣的,只不過代碼講述更加詳細。所有源碼都是來自Android4.0.4。有代碼就有真相~!~!)

類結構圖

先給大家展示一張相關類的結構圖:
HttpUrlConnection和Socket關系類圖

HttpUrlConnection 使用

在分析代碼的時候我希望首相腦海中要有一個URL的請求過程。
這是我在網上摘的一個HttpUrlConnection請求小Demo:

public class EsmTest {
    /**
     * 通過HttpURLConnection模擬post表單提交
     * @throws Exception
     */
    @Test
    public void sendEms() throws Exception {
        String wen = "MS2201828";
        String btnSearch = "EMS快遞查詢";
        URL url = new URL("http://www.kd185.com/ems.php");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");// 提交模式
        // conn.setConnectTimeout(10000);//連接超時 單位毫秒
        // conn.setReadTimeout(2000);//讀取超時 單位毫秒
        conn.setDoOutput(true);// 是否輸入參數
        StringBuffer params = new StringBuffer();
        // 表單參數與get形式一樣
        params.append("wen").append("=").append(wen).append("&")
              .append("btnSearch").append("=").append(btnSearch);
        byte[] bypes = params.toString().getBytes();
        conn.getOutputStream().write(bypes);// 輸入參數
        InputStream inStream=conn.getInputStream();
        System.out.println(new String(StreamTool.readInputStream(inStream), "gbk"));

    }

    public void sendSms() throws Exception{
        String message="貨已發到";
        message=URLEncoder.encode(message, "UTF-8");
        System.out.println(message);
        String path ="http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+message;
        URL url =new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(5*1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();    
        byte[] data = StreamTool.readInputStream(inStream);
        String result=new String(data, "UTF-8");
        System.out.println(result);
    }
}

URL產生請求

/*****************URL.java************************/
/**
 * 創建一個新的URL實例
 */
public URL(String spec) throws MalformedURLException {
    this((URL) null, spec, null);
}
public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException {
    if (spec == null) {
        throw new MalformedURLException();
    }
    if (handler != null) {
        streamHandler = handler;
    }
    spec = spec.trim();
    //獲取url的協議類型,http,https
    protocol = UrlUtils.getSchemePrefix(spec);
    //請求開始部分的位置
    int schemeSpecificPartStart = protocol != null ? (protocol.length() + 1) : 0;
    if (protocol != null && context != null && !protocol.equals(context.protocol)) {
        context = null;
    }
    if (context != null) {
        set(context.protocol, context.getHost(), context.getPort(), context.getAuthority(),
                context.getUserInfo(), context.getPath(), context.getQuery(),
                context.getRef());
        if (streamHandler == null) {
            streamHandler = context.streamHandler;
        }
    } else if (protocol == null) {
        throw new MalformedURLException("Protocol not found: " + spec);
    }
    //這裡為重點,獲取StreamHandler
    if (streamHandler == null) {
        setupStreamHandler();
        if (streamHandler == null) {
            throw new MalformedURLException("Unknown protocol: " + protocol);
        }
    }
    try {
        //對url的處理
        streamHandler.parseURL(this, spec, schemeSpecificPartStart, spec.length());
    } catch (Exception e) {
        throw new MalformedURLException(e.toString());
    }
}

void setupStreamHandler() {
    //從緩存中獲取
    streamHandler = streamHandlers.get(protocol);
    if (streamHandler != null) {
        return;
    }
    //通過工廠方法創建
    if (streamHandlerFactory != null) {
        streamHandler = streamHandlerFactory.createURLStreamHandler(protocol);
        if (streamHandler != null) {
            streamHandlers.put(protocol, streamHandler);
            return;
        }
    }
    //在同名包下檢測一個可用的hadnler
    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) {
            }
        }
    }
    //如果還是沒有創建成功那麼new一個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();
    }
    if (streamHandler != null) {
        streamHandlers.put(protocol, streamHandler);
    }
}
/**
 * streamHandler實現類為HttpURLConnectionImpl
 */
public final class HttpHandler extends URLStreamHandler {

    @Override protected URLConnection openConnection(URL u) throws IOException {
        return new HttpURLConnectionImpl(u, getDefaultPort());
    }

    @Override protected URLConnection openConnection(URL url, Proxy proxy) throws IOException {
        if (url == null || proxy == null) {
            throw new IllegalArgumentException("url == null || proxy == null");
        }
        return new HttpURLConnectionImpl(url, getDefaultPort(), proxy);
    }

    @Override protected int getDefaultPort() {
        return 80;
    }
}

創建連接請求准備

/*****************HttpURLConnectionImpl.java start************************/
/**
 * 無論是get還是post都需要建立連接
 * post
 */
@Override 
public final OutputStream getOutputStream() throws IOException {
    connect();
    OutputStream result = httpEngine.getRequestBody();
    if (result == null) {
        throw new ProtocolException("method does not support a request body: " + method);
    } else if (httpEngine.hasResponse()) {
        throw new ProtocolException("cannot write request body after response has been read");
    }
    return result;
}
/**
 * 無論是get還是post都需要建立連接
 * get
 */
@Override 
public final InputStream getInputStream() throws IOException {
    if (!doInput) {
        throw new ProtocolException("This protocol does not support input");
    }
    //獲取http響應
    HttpEngine response = getResponse();
    //返回400拋異常
    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;
}
private HttpEngine getResponse() throws IOException {
    //初始化http引擎
    initHttpEngine();
    //是否有響應頭信息
    if (httpEngine.hasResponse()) {
        return httpEngine;
    }
    try {
        while (true) {
            //發送請求
            httpEngine.sendRequest();
            httpEngine.readResponse();
            //為下次請求做准備
            Retry retry = processResponseHeaders();
            if (retry == Retry.NONE) {
                httpEngine.automaticallyReleaseConnectionToPool();
                break;
            }
            //如果一個請求不能完成那麼接下來為下次請求做准備
            String retryMethod = method;
            OutputStream requestBody = httpEngine.getRequestBody();

            /*
             * Although RFC 2616 10.3.2 specifies that a HTTP_MOVED_PERM
             * redirect should keep the same method, Chrome, Firefox and the
             * RI all issue GETs when following any redirect.
             */
            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();
            }

            httpEngine.release(true);

            httpEngine = newHttpEngine(retryMethod, rawRequestHeaders,
                    httpEngine.getConnection(), (RetryableOutputStream) requestBody);
        }
        return httpEngine;
    } catch (IOException e) {
        httpEngineFailure = e;
        throw e;
    }
}

@Override 
public final void connect() throws IOException {
    initHttpEngine();
    try {
        httpEngine.sendRequest();
    } catch (IOException e) {
        httpEngineFailure = e;
        throw e;
    }
}
/**
 * 無論是get還是post都需要初始化Http引擎
 */
private void initHttpEngine() throws IOException {
    if (httpEngineFailure != null) {
        throw httpEngineFailure;
    } else if (httpEngine != null) {
        return;
    }
    connected = true;
    try {
        if (doOutput) {
            if (method == HttpEngine.GET) {
                //如果要寫入那麼這就是一個post請求
                method = HttpEngine.POST;
            } else if (method != HttpEngine.POST && method != HttpEngine.PUT) {
                //如果你要寫入,那麼不是post請求也不是put請求那就拋異常吧。
                throw new ProtocolException(method + " does not support writing");
            }
        }
        httpEngine = newHttpEngine(method, rawRequestHeaders, null, null);
    } catch (IOException e) {
        httpEngineFailure = e;
        throw e;
    }
}

創建Socket連接

/********************HttpEngine.java**************/
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public final void sendRequest() throws IOException {
    if (responseSource != null) {
        return;
    }
    //填充請求頭和cookies
    prepareRawRequestHeaders();
    //初始化響應資源,計算緩存過期時間,判斷是否讀取緩沖中數據,或者進行網絡請求
    //responseSource = ?
    //CACHE:返回緩存信息
    //CONDITIONAL_CACHE:進行網絡請求如果網絡請求結果無效則使用緩存
    //NETWORK:返回網絡請求
    initResponseSource();
    //請求行為記錄
    if (responseCache instanceof HttpResponseCache) {
        ((HttpResponseCache) responseCache).trackResponse(responseSource);
    }
    //請求資源需要訪問網絡,但請求頭部禁止請求。在這種情況下使用BAD_GATEWAY_RESPONSE替代
    if (requestHeaders.isOnlyIfCached() && responseSource.requiresConnection()) {
        if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
            IoUtils.closeQuietly(cachedResponseBody);
        }
        this.responseSource = ResponseSource.CACHE;
        this.cacheResponse = BAD_GATEWAY_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;
    }
}
private void sendSocketRequest() throws IOException {
    if (connection == null) {
        connect();
    }
    if (socketOut != null || requestOut != null || socketIn != null) {
        throw new IllegalStateException();
    }
    socketOut = connection.getOutputStream();
    requestOut = socketOut;
    socketIn = connection.getInputStream();
    if (hasRequestBody()) {
        initRequestBodyOut();
    }
}
//打開Socket連接
protected void connect() throws IOException {
    if (connection == null) {
        connection = openSocketConnection();
    }
}
protected final HttpConnection openSocketConnection() throws IOException {
    HttpConnection result = HttpConnection.connect(
            uri, policy.getProxy(), requiresTunnel(), policy.getConnectTimeout());
    Proxy proxy = result.getAddress().getProxy();
    if (proxy != null) {
        policy.setProxy(proxy);
    }
    result.setSoTimeout(policy.getReadTimeout());
    return result;
}
/********************HttpConnection.java**************/
public static HttpConnection connect(URI uri, Proxy proxy, boolean requiresTunnel,
        int connectTimeout) throws IOException {
    //代理直連
    if (proxy != null) {
        Address address = (proxy.type() == Proxy.Type.DIRECT)
                ? new Address(uri)
                : new Address(uri, proxy, requiresTunnel);
        return HttpConnectionPool.INSTANCE.get(address, connectTimeout);
    }
    //尋找代理直連
    ProxySelector selector = ProxySelector.getDefault();
    List proxyList = selector.select(uri);
    if (proxyList != null) {
        for (Proxy selectedProxy : proxyList) {
            if (selectedProxy.type() == Proxy.Type.DIRECT) {
                // the same as NO_PROXY
                // TODO: if the selector recommends a direct connection, attempt that?
                continue;
            }
            try {
                Address address = new Address(uri, 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);
            }
        }
    }
    //創建一個直連接
    return HttpConnectionPool.INSTANCE.get(new Address(uri), connectTimeout);
}
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 {
            //DNS解析,socket連接(這塊不做詳細分析)
            socketCandidate.connect(
                    new InetSocketAddress(addresses[i], config.socketPort), connectTimeout);
            break;
        } catch (IOException e) {
            if (i == addresses.length - 1) {
                throw e;
            }
        }
    }
    this.socket = socketCandidate;
}
/********************HttpConnectionPool.java**************/
public HttpConnection get(HttpConnection.Address address, int connectTimeout)
        throws IOException {
    //首先嘗試重用現有的HTTP連接。
    synchronized (connectionPool) {
        List connections = connectionPool.get(address);
        if (connections != null) {
            while (!connections.isEmpty()) {
                HttpConnection connection = connections.remove(connections.size() - 1);
                if (!connection.isStale()) { // TODO: this op does I/O!
                    // Since Socket is recycled, re-tag before using
                    final Socket socket = connection.getSocket();
                    SocketTagger.get().tag(socket);
                    return connection;
                }
            }
            connectionPool.remove(address);
        }
    }
    //無法找到可以復用的鏈接是,創建一個新的鏈接
    return address.connect(connectTimeout);
}

/********************HttpConnection.Address.java**************/
public HttpConnection connect(int connectTimeout) throws IOException {
    return new HttpConnection(this, connectTimeout);
}

輸出內容獲取

/********************HttpEngine.java**************/
public final void readResponse() throws IOException {
    //如果有響應頭
    if (hasResponse()) {
        return;
    }
    //readResponse之前是否sendRequest
    if (responseSource == null) {
        throw new IllegalStateException("readResponse() without sendRequest()");
    }
    //如果不進行網絡請求直接返回
    if (!responseSource.requiresConnection()) {
        return;
    }
    //刷新請求頭
    if (sentRequestMillis == -1) {
        int contentLength = requestBodyOut instanceof RetryableOutputStream
                ? ((RetryableOutputStream) requestBodyOut).contentLength()
                : -1;
        writeRequestHeaders(contentLength);
    }
    //刷新請求體
    if (requestBodyOut != null) {
        requestBodyOut.close();
        if (requestBodyOut instanceof RetryableOutputStream) {
            ((RetryableOutputStream) requestBodyOut).writeToSocket(requestOut);
        }
    }

    requestOut.flush();
    requestOut = socketOut;
    //解析響應頭
    readResponseHeaders();
    responseHeaders.setLocalTimestamps(sentRequestMillis, System.currentTimeMillis());
    //判斷響應體類型
    if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
        if (cachedResponseHeaders.validate(responseHeaders)) {
            if (responseCache instanceof HttpResponseCache) {
                ((HttpResponseCache) responseCache).trackConditionalCacheHit();
            }
            //釋放資源
            release(true);
            //返回緩存信息
            setResponse(cachedResponseHeaders.combine(responseHeaders), cachedResponseBody);
            return;
        } else {
            IoUtils.closeQuietly(cachedResponseBody);
        }
    }

    if (hasResponseBody()) {
        maybeCache(); // reentrant. this calls into user code which may call back into this!
    }

    initContentStream(getTransferStream());
}
private InputStream getTransferStream() throws IOException {
    if (!hasResponseBody()) {
        return new FixedLengthInputStream(socketIn, cacheRequest, this, 0);
    }

    if (responseHeaders.isChunked()) {
        return new ChunkedInputStream(socketIn, cacheRequest, this);
    }

    if (responseHeaders.getContentLength() != -1) {
        return new FixedLengthInputStream(socketIn, cacheRequest, this,
                responseHeaders.getContentLength());
    }
    return new UnknownLengthHttpInputStream(socketIn, cacheRequest, this);
}
private void initContentStream(InputStream transferStream) throws IOException {
    //是否gzip壓縮
    if (transparentGzip && responseHeaders.isContentEncodingGzip()) {
        responseHeaders.stripContentEncoding();
        responseBodyIn = new GZIPInputStream(transferStream);
    } else {
        responseBodyIn = transferStream;
    }
}

整個請求的響應流程大概就是這樣子的,其中的涉及的路由信息獲取,DNS解析與緩存,請求的緩存過期等都還沒有仔細研讀。不過這些也夠自己消化一段時間了^_^,相信自己現在回過頭來看OkHttp的實現應該不是那麼困難了。

默默肅立的路燈,像等待檢閱的哨兵,站姿筆挺,瞪著炯炯有神的眼睛,時刻守護著這城市的安寧。一排排、一行行路燈不斷向遠方延伸,匯聚成了一支支流光溢彩的河流,偶爾有汽車疾馳而去,也是一尾尾魚兒在河裡游動。夜已深~!~!~!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved