Android--httpurlconnetion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.