zyh.net.http
Class HttpURLConnection

java.lang.Object
  |
  +--java.net.URLConnection
        |
        +--java.net.HttpURLConnection
              |
              +--zyh.net.http.HttpURLConnection

public class HttpURLConnection
extends java.net.HttpURLConnection

HttpURLConnection class implements java.net.HttpURLConnection

See Also:
HttpURLConnection

Fields inherited from class java.net.HttpURLConnection
HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD, HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT, HTTP_CONFLICT, HTTP_CREATED, HTTP_ENTITY_TOO_LARGE, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_GONE, HTTP_INTERNAL_ERROR, HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_MULT_CHOICE, HTTP_NO_CONTENT, HTTP_NOT_ACCEPTABLE, HTTP_NOT_AUTHORITATIVE, HTTP_NOT_FOUND, HTTP_NOT_MODIFIED, HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED, HTTP_PRECON_FAILED, HTTP_PROXY_AUTH, HTTP_REQ_TOO_LONG, HTTP_RESET, HTTP_SEE_OTHER, HTTP_SERVER_ERROR, HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, method, responseCode, responseMessage
 
Fields inherited from class java.net.URLConnection
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
 
Constructor Summary
HttpURLConnection(java.net.URL url)
          Constructor for HttpURLConnection object.
HttpURLConnection(java.net.URL url, java.lang.String mailbox)
          Constructor for HttpURLConnection object.
HttpURLConnection(java.net.URL url, java.lang.String mailbox, java.net.InetAddress proxyInetAddress, int proxyPort)
          Constructor for HttpURLConnection object.
 
Method Summary
 void connect()
          Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
 void disconnect()
          Close the connection to the server.
static java.lang.String getDefaultRequestProperty(java.lang.String key)
          Returns the value of the default request property.
 java.io.InputStream getErrorStream()
          Returns the error stream if the doHTTPConnect failed but the server sent useful data nonetheless.
 java.lang.String getHeaderField(int n)
          Returns the value for the nth header field.
 java.lang.String getHeaderField(java.lang.String name)
          Returns the name of the specified header field.
 java.lang.String getHeaderFieldKey(int n)
          Returns the key for the nth header field.
 java.io.InputStream getInputStream()
          Returns an input stream that reads from this open connection.
 java.io.PrintStream getLog()
          Gets the log stream.
 java.io.OutputStream getOutputStream()
          Returns an output stream that writes to this connection.
 java.lang.String getRequestProperty(java.lang.String key)
          Returns the value of the named general request property for this doHTTPConnect.
 java.net.URL getURL()
           
 void log(java.lang.String msg)
          Prints a message to the current log stream.
static void main(java.lang.String[] args)
          A simple test application
static void setDefaultRequestProperty(java.lang.String key, java.lang.String value)
          Sets the default value of a general request property.
 void setLog(java.io.PrintStream logStream)
           
 void setRequestProperty(java.lang.String key, java.lang.String value)
          Sets the general request property.
 boolean usingProxy()
          Indicates if the connection is going through a proxy.
 
Methods inherited from class java.net.HttpURLConnection
getFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setFollowRedirects, setRequestMethod
 
Methods inherited from class java.net.URLConnection
getAllowUserInteraction, getContent, getContentEncoding, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderFieldDate, getHeaderFieldInt, getIfModifiedSince, getLastModified, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setUseCaches, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HttpURLConnection

public HttpURLConnection(java.net.URL url)
                  throws java.net.MalformedURLException
Constructor for HttpURLConnection object.
Parameters:
url - the destination url

HttpURLConnection

public HttpURLConnection(java.net.URL url,
                         java.lang.String mailbox)
                  throws java.net.MalformedURLException
Constructor for HttpURLConnection object.
Parameters:
url - the destination url
mailbox - please provide your mailbox so that server maintainers can contact you in case of problems

HttpURLConnection

public HttpURLConnection(java.net.URL url,
                         java.lang.String mailbox,
                         java.net.InetAddress proxyInetAddress,
                         int proxyPort)
                  throws java.net.MalformedURLException
Constructor for HttpURLConnection object.
Parameters:
url - the destination url
mailbox - please provide your mailbox so that server maintainers can contact you in case of problems
proxyInetAddress - the InetAddress of HTTP proxy
proxyPort - the port of HTTP proxy
Method Detail

getURL

public java.net.URL getURL()
Overrides:
getURL in class java.net.URLConnection

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an input stream that reads from this open connection.
Overrides:
getInputStream in class java.net.URLConnection

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException
Returns an output stream that writes to this connection.
Overrides:
getOutputStream in class java.net.URLConnection

setLog

public void setLog(java.io.PrintStream logStream)

getLog

public java.io.PrintStream getLog()
Gets the log stream.

log

public void log(java.lang.String msg)
Prints a message to the current log stream.
Parameters:
message - a log or tracing message

usingProxy

public boolean usingProxy()
Indicates if the connection is going through a proxy.
Overrides:
usingProxy in class java.net.HttpURLConnection

disconnect

public void disconnect()
Close the connection to the server.
Overrides:
disconnect in class java.net.HttpURLConnection

connect

public void connect()
             throws java.io.IOException
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
Overrides:
connect in class java.net.URLConnection

getHeaderField

public java.lang.String getHeaderField(java.lang.String name)
Returns the name of the specified header field.
Overrides:
getHeaderField in class java.net.URLConnection
Parameters:
name - the name of a header field.
Returns:
the value of the named header field, or null if there is no such field in the header.

getHeaderFieldKey

public java.lang.String getHeaderFieldKey(int n)
Returns the key for the nth header field.
Overrides:
getHeaderFieldKey in class java.net.URLConnection
Parameters:
n - an index.
Returns:
the key for the nth header field, or null if there are fewer than n fields.

getHeaderField

public java.lang.String getHeaderField(int n)
Returns the value for the nth header field. It returns null if there are fewer than n fields.

This method can be used in conjunction with the getHeaderFieldKey method to iterate through all the headers in the message.

Overrides:
getHeaderField in class java.net.URLConnection
Parameters:
n - an index.
Returns:
the value of the nth header field.
See Also:
URLConnection.getHeaderFieldKey(int)

getRequestProperty

public java.lang.String getRequestProperty(java.lang.String key)
Returns the value of the named general request property for this doHTTPConnect.
Overrides:
getRequestProperty in class java.net.URLConnection
Returns:
the value of the named general request property for this doHTTPConnect.

setRequestProperty

public void setRequestProperty(java.lang.String key,
                               java.lang.String value)
Sets the general request property.
Overrides:
setRequestProperty in class java.net.URLConnection
Parameters:
key - the keyword by which the request is known (e.g., "accept").
value - the value associated with it.

setDefaultRequestProperty

public static void setDefaultRequestProperty(java.lang.String key,
                                             java.lang.String value)
Sets the default value of a general request property. When a URLConnection is created, it is initialized with these properties.
Parameters:
key - the keyword by which the request is known (e.g., "accept").
value - the value associated with the key.

getDefaultRequestProperty

public static java.lang.String getDefaultRequestProperty(java.lang.String key)
Returns the value of the default request property. Default request properties are set for every doHTTPConnect.
Returns:
the value of the default request property for the specified key.
See Also:
URLConnection.setDefaultRequestProperty(java.lang.String, java.lang.String)

getErrorStream

public java.io.InputStream getErrorStream()
Returns the error stream if the doHTTPConnect failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.

This method will not cause a doHTTPConnect to be initiated. If there the doHTTPConnect was not connected, or if the server did not have an error while connecting or if the server did have an error but there no error data was sent, this method will return null. This is the default.

Overrides:
getErrorStream in class java.net.HttpURLConnection
Returns:
an error stream if any, null if there have been no errors, the doHTTPConnect is not connected or the server sent no useful data.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
A simple test application