Abstract Class HttpURLConnection in Java

digitization, transformation, binary

Abstract Class HttpURLConnection in Java

HttpURLConnection class is a specialization of URLConnection. An instance of this class is returned when the openConnection() method is called for a URL object that uses the HTTP protocol.

The many constants defined by this class are the status codes returned by HTTP servers. setRequestMethod() specifies what kind of HTTP request is made. The contents of this request must be sent through the OutputStream returned by the getOutputStream() method of the superclass. Once an HTTP request has been sent, getResponseCode() returns the
HTTP server’s response code as an integer, and getResponseMessage() returns the server’s response message.

Ads code goes here

The disconnect() method closes the connection, and the static setFollowRedirects() specifies whether URL connections that use the HTTP protocol should automatically follow redirect responses sent by HTTP servers.

The HttpURLConnection class is an abstract class that is a subclass of URLConnection. HttpURLConnection defines many of the HTTP server response codes as constants and provides methods for parsing server responses.

An HttpURLConnection object defines a network connection to a resource specified by a URL. Essentially, the HttpURLConnection object represents the HTTP request for that resource.

The structure of the class HttpURLConnection is given by

public abstract class java.net.HttpURLConnection extends java.net.URLConnection{
// Protected Constructor
protected HttpURLConnection(URL u);
// Response Code Constants
public static final int HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD;
public static final int HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT,HTTP_CONFLICT;
public static final int HTTP_CREATED, HTTP_ENTITY_TOO_LARGE,HTTP_FORBIDDEN;
public static final int HTTP_GATEWAY_TIMEOUT, HTTP_GONE,HTTP_INTERNAL_ERROR;
public static final int HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM,HTTP_MOVED_TEMP;
public static final int HTTP_MULT_CHOICE, HTTP_NOT_ACCEPTABLE,HTTP_NOT_AUTHORITATIVE;
public static final int HTTP_NOT_FOUND, HTTP_NOT_MODIFIED,HTTP_NO_CONTENT;
public static final int HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED;
public static final int HTTP_PRECON_FAILED, HTTP_PROXY_AUTH,HTTP_REQ_TOO_LONG;
public static final int HTTP_RESET, HTTP_SEE_OTHER, HTTP_SERVER_ERROR;
public static final int HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE,HTTP_UNSUPPORTED_TYPE;
public static final int HTTP_USE_PROXY, HTTP_VERSION;
// Protected Instance Variables
protected String method;
protected int responseCode;
protected String responseMessage;
// Class Methods
public static boolean getFollowRedirects();
public static void setFollowRedirects(boolean set);
// Public Instance Methods
public abstract void disconnect();
public String getRequestMethod();
public int getResponseCode() throws IOException;
public String getResponseMessage() throws IOException;
public void setRequestMethod(String method) throws ProtocolException;
public abstract boolean usingProxy();
}

The details of the class structure are given as follows:

public final static int HTTP_ACCEPTED;

public final static int HTTP_ACCEPTED appears for the HTTP response code that means the request has been accepted by the server. HTTP_ACCEPTED = 202

public final static int HTTP_BAD_GATEWAY;

public final static int HTTP_BAD_GATEWAY appears for the HTTP response code that means the server, acting as a gateway, has received a bad response from another server. HTTP_BAD_GATEWAY = 502

public final static int HTTP_BAD_METHOD;

public final static int HTTP_BAD_METHOD appears for the HTTP response code that means the requested method is not allowed for the requested resource. HTTP_BAD_METHOD = 405

public final static int HTTP_BAD_REQUEST;

public final static int HTTP_BAD_REQUEST appears for the HTTP response code that means the request was syntactically incorrect. HTTP_BAD_REQUEST = 400

public final static int HTTP_CLIENT_TIMEOUT;

public final static int HTTP_CLIENT_TIMEOUT appears for the HTTP response code that means the server has not received a request from the client in the time it expected. HTTP_CLIENT_TIMEOUT = 408

public final static int HTTP_CONFLICT;

public final static int HTTP_CONFLICT appears for the HTTP response code that means there is a conflict with the state of the requested resource. HTTP_CONFLICT = 409

public final static int HTTP_CREATED;

public final static int HTTP_CREATED appears for the HTTP response code that means a new resource has been created as the result of the request. HTTP_CREATED = 201

public final static int HTTP_ENTITY_TOO_LARGE;

public final static int HTTP_ENTITY_TOO_LARGE appears for the HTTP response code that means the request contains an entity that is too large for the server. HTTP_ENTITY_TOO_LARGE = 413

public final static int HTTP_FORBIDDEN;

public final static int HTTP_FORBIDDEN appears for the HTTP response code that means the client does not have permission to access the requested resource. HTTP_FORBIDDEN = 403

public final static int HTTP_GATEWAY_TIMEOUT;

public final static int HTTP_GATEWAY_TIMEOUT appears for the HTTP response code that means the server, acting as a gateway, has not received a response from an upstream server in the time it expected. HTTP_GATEWAY_TIMEOUT = 504

public final static int HTTP_GONE;

public final static int HTTP_GONE appears for the HTTP response code that means the requested resource is no longer available. HTTP_GONE = 410

public final static int HTTP_INTERNAL_ERROR;

public final static int HTTP_INTERNAL_ERROR appears for the HTTP response code that means the server does not or cannot support the client’s request. HTTP_INTERNAL_ERROR = 501

public final static int HTTP_LENGTH_REQUIRED;

public final static int HTTP_LENGTH_REQUIRED appears for the HTTP response code that means the server won’t accept the request without a length indication. HTTP_LENGTH_REQUIRED = 411

public final static int HTTP_MOVED_PERM;

public final static int HTTP_MOVED_PERM appears for the HTTP response code that means the requested resource has moved permanently. HTTP_MOVED_PERM = 301

public final static int HTTP_MOVED_TEMP;

public final static int HTTP_MOVED_TEMP appears for the HTTP response code that means the requested resource has moved temporarily. HTTP_MOVED_TEMP = 302

public final static int HTTP_MULT_CHOICE;

public final static int HTTP_MULT_CHOICE appears for the HTTP response code that means the requested resource is available in multiple representations. HTTP_MULT_CHOICE = 300

public final static int HTTP_NOT_ACCEPTABLE;

public final static int HTTP_NOT_ACCEPTABLE appears for the HTTP response code that means the requested resource is not available in a representation that is acceptable to the
client. HTTP_NOT_ACCEPTABLE = 406

public final static int HTTP_NOT_AUTHORITATIVE;

public final static int HTTP_NOT_AUTHORITATIVE appears for the HTTP response code that means the information returned may be a copy. HTTP_NOT_AUTHORITATIVE = 203

public final static int HTTP_NOT_FOUND;

public final static int HTTP_NOT_FOUND appears for the HTTP response code that means the server could not find the requested resource. HTTP_NOT_FOUND = 404

public final static int HTTP_NOT_MODIFIED;

public final static int HTTP_NOT_MODIFIED appears for the HTTP response code that means the requested resource has not been modified. HTTP_NOT_MODIFIED = 304

public final static int HTTP_NO_CONTENT;

public final static int HTTP_NO_CONTENT appears for the HTTP response code that means the request has been processed, but there is no new information. HTTP_NO_CONTENT = 204

public final static int HTTP_OK;

public final static int HTTP_OK appears for the HTTP response code that means the request succeeded. HTTP_OK = 200

public final static int HTTP_PARTIAL;

public final static int HTTP_PARTIAL appears for the HTTP response code that means the partial request has been fulfilled. HTTP_PARTIAL = 206

public final static int HTTP_PAYMENT_REQUIRED;

public final static int HTTP_PAYMENT_REQUIRED appears for an HTTP response code that is reserved for future use. HTTP_PAYMENT_REQUIRED = 402

public final static int HTTP_PRECON_FAILED;

public final static int HTTP_PRECON_FAILED appears for the HTTP response code that means the precondition in the request has failed. HTTP_PRECON_FAILED = 412

public final static int HTTP_PROXY_AUTH;

public final static int HTTP_PROXY_AUTH appears for the HTTP response code that means the client needs to authenticate itself with the proxy. HTTP_PROXY_AUTH = 407

public final static int HTTP_REQ_TOO_LONG;

public final static int HTTP_REQ_TOO_LONG appears for the HTTP response code that means the client request is too long. HTTP_REQ_TOO_LONG = 414

public final static int HTTP_RESET;

public final static int HTTP_RESET appears for the HTTP response code that means the request has been fulfilled, and the client should reset its view. HTTP_RESET = 205

public final static int HTTP_SEE_OTHER;

public final static int HTTP_SEE_OTHER appears for the HTTP response code that means the requested resource is available at another URL. HTTP_SEE_OTHER = 303

public final static int HTTP_SERVER_ERROR;

public final static int HTTP_SERVER_ERROR appears for the HTTP response code that means the server encountered a problem and could not fulfill the request. HTTP_SERVER_ERROR = 500

public final static int HTTP_UNAUTHORIZED;

public final static int HTTP_UNAUTHORIZED appears for the HTTP response code that means the client is not authenticated for the requested resource. HTTP_UNAUTHORIZED = 401

public final static int HTTP_UNAVAILABLE;

public final static int HTTP_UNAVAILABLE appears for the HTTP response code that means the server is temporarily unable to fulfill the request. HTTP_UNAVAILABLE = 503

public final static int HTTP_UNSUPPORTED_TYPE;

public final static int HTTP_UNSUPPORTED_TYPE appears for the HTTP response code that means the server cannot process the type of the request. HTTP_UNSUPPORTED_TYPE = 415

public final static int HTTP_USE_PROXY;

public final static int HTTP_USE_PROXY appears for the HTTP response code that means a proxy must be used to access the requested resource. HTTP_USE_PROXY = 305

public final static int HTTP_VERSION;

public final static int HTTP_VERSION appears for The HTTP response code that means the server does not support the HTTP version used in the request.HTTP_VERSION = 505

protected String method;

protected String method appears for the method of this request. Valid values are: “DELETE”, “GET”, “HEAD”, “OPTIONS”, “POST”, “PUT”, and “TRACE”. method = “GET”

protected int responseCode;

protected int responseCode appears for the response code from the server, which may be one of the HTTP_ constants. responseCode = -1

protected String responseMessage;

protected String responseMessage appears for the response message from the server that corresponds to responseCode. responseMessage = “ok”

protected HttpURLConnection(URL u);

protected HttpURLConnection(URL u) constructor creates an HttpURLConnection for the given URL object.

READ  Learn Object Serialization in Java in Quick and Easy Manner in 25 Minutes

Parameter
u – A URL object that represents a resource.

public static boolean getFollowRedirects();

public static boolean getFollowRedirects() method indicates whether or not this HttpURLConnection follows HTTP redirects. The default value is false.

This method returns a boolean value that indicates whether or not HTTP redirect codes are automatically followed.

public static void setFollowRedirects(boolean set);

public static void setFollowRedirects(boolean set) method specifies whether or not this HttpURLConnection follows HTTP redirects.

Parameter
set – A boolean value that specifies whether or not HTTP redirects should be followed.

public abstract void disconnect();

public abstract void disconnect() method closes the connection to the server.

public String getRequestMethod();

public String getRequestMethod() method method returns the method of this request.

This method returns the method of this request.

public int getResponseCode();

public int getResponseCode() method returns the code the server sent in response to this request. For example, suppose the server response is: HTTP/1.0 404 Not Found In this case, the method returns integer value 404.

This method returns the response code from the server.

public String getResponseMessage();

public String getResponseMessage() method returns the message the server sent in response to this request. For example, suppose the server response is:HTTP/1.0 404 Not Found In this case, the method returns the string “Not Found”.

This method returns the response message from the server.

public void setRequestMethod(String method);

public void setRequestMethod(String method) method method sets the method of this request. Valid values are: “DELETE”, “GET”, “HEAD”, “OPTIONS”, “POST”, “PUT”, and “TRACE”.

Parameter
method – The new method for this request.

public abstract boolean usingProxy();

public abstract boolean usingProxy() method returns a flag that indicates if this connection is going through a proxy or not.

READ  Top 20 Programming Practice in Java

This method returns a boolean value that indicates whether or not this HttpURLConnection is using a proxy.

Apart from these methods, the HttpURLConnection class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

The HttpURLConnection class also has inherited methods from URLConnection. They are as follows:

  • getAllowUserInteraction()
  • getContent()
  • getContentLength()
  • getDate()
  • getDoInput()
  • connect()
  • getContentEncoding()
  • getContentType()
  • getDefaultUseCaches()
  • getDoOutput()
  • getExpiration()
  • getHeaderField(int)
  • getHeaderFieldInt(String, int)
  • getIfModifiedSince()
  • getLastModified()
  • getRequestProperty(String)
  • getUseCaches()
  • setAllowUserInteraction(boolean)
  • setDoInput(boolean)
  • setIfModifiedSince(long)
  • setUseCaches(boolean)
  • getHeaderField(String)
  • getHeaderFieldDate(String,long)
  • getHeaderFieldKey(int)
  • getInputStream()
  • getOutputStream()
  • getURL()
  • setDefaultUseCaches(boolean)
  • setDoOutput(boolean)
  • setRequestProperty(String,String)
  • toString()

The HttpURLConnection class also has inherited variables from URLConnection. They are as follows:

  • allowUserInteraction
  • doInput
  • ifModifiedSince
  • useCaches
  • connected
  • doOutput
  • url

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *