1.HttpClientåHttpURLConnectionçåºå«
HttpClientåHttpURLConnectionçåºå«
源码先生æ»ç»äºç½ä¸çä¸äºèµæºï¼ä¸»è¦æ以ä¸ä¸¤ä¸ªè§ç¹ï¼
源码先生åæä¸ï¼
源码先生å¨ç 究Volleyæ¡æ¶çæºç ä¸ï¼åç°å®å¨HTTP请æ±ç使ç¨ä¸æ¯è¾æææï¼å¨Android 2.3å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpURLConnectionï¼èå¨Android 2.2å以ä¸çæ¬ï¼ä½¿ç¨çæ¯HttpClientãæä¹æ¯è¾å¥½å¥è¿ä¹ä½¿ç¨çåå ï¼äºæ¯ä¸é¨æ¾å°äºä¸ä½Googleçå·¥ç¨å¸åçä¸ç¯å客ï¼æä¸å¯¹HttpURLConnectionåHttpClientè¿è¡äºå¯¹æ¯ï¼ä¸é¢æå°±ç»å¤§å®¶ç®è¦å°ç¿»è¯ä¸ä¸ã
源码先生大å¤æ°çAndroidåºç¨ç¨åºé½ä¼ä½¿ç¨HTTPåè®®æ¥åéåæ¥æ¶ç½ç»æ°æ®ï¼èAndroidä¸ä¸»è¦æä¾äºä¸¤ç§æ¹å¼æ¥è¿è¡HTTPæä½ï¼HttpURLConnectionåHttpClientãè¿ä¸¤ç§æ¹å¼é½æ¯æHTTPSåè®®ã以æµçå½¢å¼è¿è¡ä¸ä¼ åä¸è½½ãé ç½®è¶ æ¶æ¶é´ãIPv6ã以åè¿æ¥æ± çåè½ã
源码先生HttpClient:
源码先生DefaultHttpClientåå®çå å¼AndroidHttpClienté½æ¯HttpClientå ·ä½çå®ç°ç±»ï¼å®ä»¬é½æ¥æä¼å¤çAPIï¼èä¸å®ç°æ¯è¾ç¨³å®ï¼bugæ°éä¹å¾å°ã
源码先生ä½åæ¶ä¹ç±äºHttpClientçAPIæ°éè¿å¤ï¼ä½¿å¾æ们å¾é¾å¨ä¸ç ´åå ¼å®¹æ§çæ åµä¸å¯¹å®è¿è¡å级åæ©å±ï¼æ以ç®åAndroidå¢éå¨æååä¼åHttpClientæ¹é¢çå·¥ä½æ度并ä¸ç§¯æã
源码先生HttpURLConnection:
源码先生HttpURLConnectionæ¯ä¸ç§å¤ç¨éãè½»éæçHTTP客æ·ç«¯ï¼ä½¿ç¨å®æ¥è¿è¡HTTPæä½å¯ä»¥éç¨äºå¤§å¤æ°çåºç¨ç¨åºãè½ç¶HttpURLConnectionçAPIæä¾çæ¯è¾ç®åï¼ä½æ¯åæ¶è¿ä¹ä½¿å¾æ们å¯ä»¥æ´å 容æå°å»ä½¿ç¨åæ©å±å®ã
源码先生ä¸è¿å¨Android 2.2çæ¬ä¹åï¼HttpURLConnectionä¸ç´åå¨çä¸äºä»¤äººåç¦çbugãæ¯å¦è¯´å¯¹ä¸ä¸ªå¯è¯»çInputStreamè°ç¨close()æ¹æ³æ¶ï¼å°±æå¯è½ä¼å¯¼è´è¿æ¥æ± 失æäºãé£ä¹æ们é常ç解å³åæ³å°±æ¯ç´æ¥ç¦ç¨æè¿æ¥æ± çåè½ï¼
源码先生[java] view plaincopy
源码先生[java] view plaincopy
源码先生private void disableConnectionReuseIfNecessary() {
源码先生// è¿æ¯ä¸ä¸ª2.2çæ¬ä¹åçbug
源码先生if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
源码先生System.setProperty(". ä¸ä½¿ç¨å¾æå¤ãæéè¦çåè®®äºï¼è¶æ¥è¶å¤ç Java åºç¨ç¨åºéè¦ç´æ¥éè¿ HTTP åè®®æ¥è®¿é®ç½ç»èµæºãå¨ JDK ç java.net å ä¸å·²ç»æä¾äºè®¿é® HTTP åè®®çåºæ¬åè½ï¼HttpURLConnectionã
源码先生HttpURLConnectionæ¯javaçæ åç±»ï¼HttpURLConnection继æ¿èªURLConnectionï¼å¯ç¨äºåæå®ç½ç«åéGET请æ±ãPOST请æ±ãå®å¨URLConnectionçåºç¡ä¸æä¾äºå¦ä¸ä¾¿æ·çæ¹æ³ï¼
源码先生int getResponseCode()ï¼è·åæå¡å¨çååºä»£ç ã
源码先生String getResponseMessage()ï¼è·åæå¡å¨çååºæ¶æ¯ã
源码先生String getResponseMethod()ï¼è·ååé请æ±çæ¹æ³ã
源码先生void setRequestMethod(String method)ï¼è®¾ç½®åé请æ±çæ¹æ³ã
源码先生å¨ä¸è¬æ åµä¸ï¼å¦æåªæ¯éè¦Webç«ç¹çæ个ç®å页é¢æ交请æ±å¹¶è·åæå¡å¨ååºï¼HttpURLConnectionå®å ¨å¯ä»¥èä»»ãä½å¨ç»å¤§é¨åæ åµä¸ï¼Webç«ç¹çç½é¡µå¯è½æ²¡è¿ä¹ç®åï¼è¿äºé¡µé¢å¹¶ä¸æ¯éè¿ä¸ä¸ªç®åçURLå°±å¯è®¿é®çï¼å¯è½éè¦ç¨æ·ç»å½èä¸å ·æç¸åºçæéæå¯è®¿é®è¯¥é¡µé¢ãå¨è¿ç§æ åµä¸ï¼å°±éè¦æ¶åSessionãCookieçå¤çäºï¼å¦ææç®ä½¿ç¨HttpURLConnectionæ¥å¤çè¿äºç»èï¼å½ç¶ä¹æ¯å¯è½å®ç°çï¼åªæ¯å¤çèµ·æ¥é¾åº¦å°±å¤§äºã
源码先生为äºæ´å¥½å°å¤çåWebç«ç¹è¯·æ±ï¼å æ¬å¤çSessionãCookieçç»èé®é¢ï¼Apacheå¼æºç»ç»æä¾äºä¸ä¸ªHttpClient项ç®ï¼çå®çå称就ç¥éï¼å®æ¯ä¸ä¸ªç®åçHTTP客æ·ç«¯ï¼å¹¶ä¸æ¯æµè§å¨ï¼ï¼å¯ä»¥ç¨äºåéHTTP请æ±ï¼æ¥æ¶HTTPååºãä½ä¸ä¼ç¼åæå¡å¨çååºï¼ä¸è½æ§è¡HTML页é¢ä¸åµå ¥çJavascript代ç ï¼ä¹ä¸ä¼å¯¹é¡µé¢å 容è¿è¡ä»»ä½è§£æãå¤çã
源码先生ç®åæ¥è¯´ï¼HttpClientå°±æ¯ä¸ä¸ªå¢å¼ºççHttpURLConnectionï¼HttpURLConnectionå¯ä»¥åçäºæ HttpClientå ¨é¨å¯ä»¥åï¼HttpURLConnection没ææä¾çæäºåè½ï¼HttpClientä¹æä¾äºï¼ä½å®åªæ¯å ³æ³¨äºå¦ä½åé请æ±ãæ¥æ¶
源码先生ååºï¼ä»¥å管çHTTPè¿æ¥ã
源码先生使ç¨HttpClientåé请æ±ãæ¥æ¶ååºå¾ç®åï¼åªè¦å¦ä¸å æ¥å³å¯ã
源码先生å建HttpClient对象ã
源码先生å¦æéè¦åéGET请æ±ï¼å建HttpGet对象ï¼å¦æéè¦åéPOST请æ±ï¼å建HttpPost对象ã
源码先生å¦æéè¦åé请æ±åæ°ï¼å¯è°ç¨HttpGetãHttpPostå ±åçsetParams(HetpParams params)æ¹æ³æ¥æ·»å 请æ±åæ°ï¼å¯¹äºHttpPost对象èè¨ï¼ä¹å¯è°ç¨setEntity(HttpEntity entity)æ¹æ³æ¥è®¾ç½®è¯·æ±åæ°ã
源码先生è°ç¨HttpClient对象çexecute(HttpUriRequest request)åé请æ±ï¼æ§è¡è¯¥æ¹æ³è¿åä¸ä¸ªHttpResponseã
源码先生è°ç¨HttpResponseçgetAllHeaders()ãgetHeaders(String name)çæ¹æ³å¯è·åæå¡å¨çååºå¤´ï¼è°ç¨HttpResponseçgetEntity()æ¹æ³å¯è·åHttpEntity对象ï¼è¯¥å¯¹è±¡å è£ äºæå¡å¨çååºå 容ãç¨åºå¯éè¿è¯¥å¯¹è±¡è·åæå¡å¨çååºå 容ã
源码先生å¦å¤ï¼Androidå·²ç»æåå°éæäºHttpClientï¼è¿æå³çå¼å人åå¯ä»¥ç´æ¥å¨Androidåºç¨ä¸ä½¿ç¨Httpclientæ¥è®¿é®æ交请æ±ãæ¥æ¶ååºã
源码先生æ¯å¦ä¸ä¸ªAndroidåºç¨éè¦åæå®é¡µé¢åé请æ±ï¼ä½è¯¥é¡µé¢å¹¶ä¸æ¯ä¸ä¸ªç®åç页é¢ï¼åªæå½ç¨æ·å·²ç»ç»å½ï¼èä¸ç»å½ç¨æ·çç¨æ·åæææ¶æå¯è®¿é®è¯¥é¡µé¢ãå¦æ使ç¨HttpURLConnectionæ¥è®¿é®è¿ä¸ªè¢«ä¿æ¤ç页é¢ï¼é£ä¹éè¦å¤ççç»è就太å¤æäºã
源码先生å ¶å®è®¿é®Webåºç¨ä¸è¢«ä¿æ¤ç页é¢ï¼ä½¿ç¨æµè§å¨åååç®åï¼ç¨æ·éè¿ç³»ç»æä¾çç»å½é¡µé¢ç»å½ç³»ç»ï¼æµè§å¨ä¼è´è´£ç»´æ¤ä¸æå¡å¨ä¹é´çSesionï¼å¦æç¨æ·ç»å½çç¨æ·åãå¯ç 符åè¦æ±ï¼å°±å¯ä»¥è®¿é®è¢«ä¿æ¤èµæºäºã
源码先生å¨Androidåºç¨ç¨åºä¸ï¼åå¯ä½¿ç¨HttpClientæ¥ç»å½ç³»ç»ï¼åªè¦åºç¨ç¨åºä½¿ç¨åä¸ä¸ªHttpClientåé请æ±ï¼HttpClientä¼èªå¨ç»´æ¤ä¸æå¡å¨ä¹é´çSessionç¶æï¼ä¹å°±æ¯è¯´ç¨åºç¬¬ä¸æ¬¡ä½¿ç¨HttpClientç»å½ç³»ç»åï¼æ¥ä¸æ¥ä½¿ç¨HttpClientå³å¯è®¿é®è¢«ä¿æ¤é¡µèäºã
源码先生