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