Java-httpurlconnection Detailed description and examples

Source: Internet
Author: User

The URLConnection class is an abstract class that represents a communication connection between an application and a URL, and an instance of this class can be used to read and write resources referenced by this URL. URLConnection allows request data to be sent to the server using Get,post or other HTTP method requests. Using URLConnection objects is generally divided into the following 7 steps.

1: Create a URL object;

2: Create the URLConnection object through the OpenConnection method of the URL object;

3: You can set parameters and general request properties by using the methods provided by the URLConnection object. Common request properties are set in the following ways:

~public void Setrequestproperty (String key,string value) sets the value corresponding to the specified request keyword

~public void Setdoinput (Boolean doinput) setting whether to use a URL connection for input with a default value of True

~public void Setdooutput (Boolean dooutput) sets whether to use a URL connection for output, the default value is False, and if set to true, you can get a byte output stream for sending data to the server

~public void Setusecaches (Boolean usecaches) sets whether this connection uses any available cache, with a default value of True

4: Call the Connect method of the URLConnection object to connect to the remote resource

5: After connecting to the server, you can query header information, query header information commonly used methods are the following:

~public string Getheaderfield (string name) returns the value of the specified header field

~public Map<string,list<string>>getheaderfields () returns a non-modifiable Map of the header field

~public String getContentType () returns the value of the Content-type header field

~public String getcontentencoding () returns the value of Content-encoding

6: Gets the input stream to access the resource data. Use the getInputStream method to get a byte input stream to read the resource information

7: Get output stream and write data

Java-httpurlconnection Detailed description and examples

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.