any network connection needs to go through the socket to connect, HttpURLConnection does not need to set the socket, so httpurlconnection is not the underlying connection, but a request on the underlying connection . That's why Httpurlconneciton is just a Abstract class , The reason why it cannot be instantiated by itself . HttpURLConnection only through URLs. OpenConnection () method to create a specific instance.
HttpClient is an interface, with Defaulthttpclient (); To create an instantiation.
Although the underlying network connection can be shared by multiple httpurlconnection instances, only one request can be sent per HttpURLConnection instance. After the request is finished, you should call the close () method of the HttpURLConnection instance's InputStream or OutputStream to free the requested network resources, but this is useless for persistent connections. For persistent connections, the socket of the underlying connection must be closed with the disconnect () method.
Httpurlconnection.setrequestmethod ("POST"); Set Request mode httpurlconnection.connect (); Connection, no need to use getInputStream instead
// Send request Parameters OutputStream outputstream = conn.getoutputstream (); Outputstream.write (entity);
// sets the content type of the transfer to be a serializable Java object (if this item is not set, java.io may be thrown when the serialization object is passed, when the Web service defaults to this type. ) Eofexception
Ways to set Connection parameters
// sets whether to read in from HttpURLConnection, the default is Truesetdooutput // set whether to httpurlconnection input, POST request, The parameter is in the HTTP body, so to set to true, by default is Falsesetifmodifiedsince setusecaches//Set whether the cache Post request cannot be cached Setdefaultallow Userinteraction setdefaultusecaches
Set Request Header Parameters
HTTP requests allow a key to take multiple values separated by commas
setrequestproperty ("Charsert", "UTF-8"); All Valuesaddrequestproperty (Key,value)//That overwrite the existing key will continue to add other value based on the original key.
The difference between Setrequestproperty and Addrequestproperty is that setrequestproperty, with the effect of 0 re-assignment. And Addrequestproperty is sending a URL request
Getoutputstream //For the POST request, pass the request parameter to the server
Get response
getcontent After the request succeeds, you can get the data in the response Getheaderfield the most getinputstream of the red flag
Get the response header information header
Getcontentencodinggetcontentlengthgetcontenttypegetdategetexpirationgetlastmodifed
Android--httpurlconnetion