Beruflich Dokumente
Kultur Dokumente
ATIJ
2-1/18
Overview
HTTP-support for Client-Side Applications through the following classes:
URL
URLConnection
HttpURLConnection
ATIJ
2-2/18
URL Connections
Client-side support for accessing and retrieving web resources.
Encapsulate much of the low-level (TCP/IP stack) complexity involved in accessing
web resources.
Support for URL connections is provided in the java.net package by the following
important classes:
URL
URLConnection
HttpURLConnection
ATIJ
2-3/18
ATIJ
2-4/18
Note that an URL instance need not represent a valid resource, but it must contain the
following components: protocol, hostname and pathname.
ATIJ
2-5/18
URL Constructors
All constructors throw a java.net.MalformedURLException if the protocol is missing
or unknown.
If the port is not specified, the default port for the protocol is assumed.
When constructing a URL, an appropriate stream protocol handler
(URLStreamHandler) is automatically loaded.
ATIJ
Constructor
Example
URL(String urlStr)
throws
MalformedURLException
URL(String protocol,
String hostname,
String filename)
throws
MalformedURLException
2-6/18
Constructor
Example
URL(String protocol,
String hostname,
int portNumber,
String filename)
throws
MalformedURLException
URL(URL context,
String spec)
throws
MalformedURLException
ATIJ
2-7/18
getProtocol()
getHost()
getPort()
getFile()
getPath()
getQuery()
getRef()
ATIJ
2-8/18
openConnection().getContent().
See FetchResourceViaMethodgetContent.java.
See also class URLConnection.
The URL class only provides an input stream to retrieve the contents of the resource.
Other information about the request sent or the response received is not accessible.
ATIJ
2-9/18
ATIJ
2-10/18
void setAllowUserInteraction(
boolean allow)
ATIJ
2-11/18
ATIJ
String getHeaderFieldKey(int n)
String getHeaderField(int n)
String getHeaderField(
String field)
Map getHeaderFields()
String
String
String
String
String
String
getContentLength()
getContentType()
getContentEncoding()
getDate()
getExpiration()
getLastModified()
2-12/18
ATIJ
2-13/18
4. Establish a connection to the remote resource, which also sends the request.
A response will be issued by the server.
connection.connect();
ATIJ
2-14/18
ATIJ
2-15/18
ATIJ
2-16/18
ATIJ
2-17/18
String getRequestMethod()
void connect()
int getResponseCode()
throws IOException
String getResponseMessage()
throws IOException
void disconnect()
2-18/18