Httpurlconnection is a standard Java class.
Httpclient is a sub-project under Apache Jakarta common. It is used to provide an efficient, up-to-date, and function-rich client programming toolkit that supports http protocol. It also supports the latest versions and suggestions of HTTP protocol. Httpclient has been applied in many projects. For example, httpclient is used by two well-known open-source projects, cactus and htmlunit. For more applications that use httpclient, see container. The httpclient project is very active and many people are using it. Currently, the httpclient version is 3.0 RC4 released in 5.10.11.
Httpclient is a good open-source framework that encapsulates HTTP request headers, parameters, content bodies, responses, and so on,
Httpurlconnection is a standard Java class with nothing encapsulated. it is inconvenient to use it in Taiyuan, suchCustom re-AccessAnd some advanced functions.
1. httpurlconnection get Method
Try {URL url = new URL ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("get"); // get the request response code int code = Conn. getresponsecode (); If (code = httpstatus. SC _ OK) {inputstream = Conn. getinputstream (); miv. setimagebitmap (bitmapfactory. decodestream (inputstream); toast. maketext (this, "get access successful", toast. length_short ). show () ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}
2. httpurlconnection POST method
Try {URL url = new URL ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setconnecttimeout (5000); Conn. setrequestmethod ("Post"); // get the request response code int code = Conn. getresponsecode (); If (code = httpstatus. SC _ OK) {inputstream = Conn. getinputstream (); miv. setimagebitmap (bitmapfactory. decodestream (inputstream); toast. maketext (this, "post access successful", toast. length_short ). show () ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}
3. httpclient get Method
Try {defaulthttpclient httpclient = new defaulthttpclient (); httpparams Params = httpclient. getparams (); httpget = new httpget ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpconnectionparams. setconnectiontimeout (Params, 5000); httpresponse response = httpclient.exe cute (httpget); If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {byte [] bytearray = entityutils. tobytearray (response. getentity (); miv. setimagebitmap (bitmapfactory. decodebytearray (bytearray, 0, bytearray. length); toast. maketext (this, "get access successful", toast. length_short ). show () ;}} catch (exception e) {e. printstacktrace ();}
4. httpclient POST method
Try {defaulthttpclient httpclient = new defaulthttpclient (); httpparams Params = httpclient. getparams (); httppost = new httppost ("http://www.baidu.com/img/baidu_sylogo1.gif"); httpconnectionparams. setconnectiontimeout (Params, 5000); httpresponse response = httpclient.exe cute (httppost); If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK) {byte [] bytearray = entityutils. tobytearray (response. getentity (); miv. setimagebitmap (bitmapfactory. decodebytearray (bytearray, 0, bytearray. length); toast. maketext (this, "post access successful", toast. length_short ). show () ;}} catch (exception e) {e. printstacktrace ();}
Mobile development QQ group: 59516399
Csdn download link: http://download.csdn.net/detail/wenwei19861106/4981424