org.javalite.http
Class Request<T extends Request>

java.lang.Object
  extended by org.javalite.http.Request<T>
Direct Known Subclasses:
Delete, Get, Post, Put

public abstract class Request<T extends Request>
extends Object

This class provides static convenience methods for simple HTTP requests.

Author:
Igor Polevoy

Field Summary
protected  HttpURLConnection connection
           
protected  String url
           
 
Constructor Summary
Request(String url, int connectTimeout, int readTimeout)
           
 
Method Summary
 T basic(String user, String password)
          Sets a user and password for basic authentication.
 byte[] bytes()
          Fetches response content from server as bytes.
protected  T connect()
           
 void dispose()
          This method is already called from text() and bytes(), you do not have to call it if you use those methods.
protected abstract  T doConnect()
          Makes a connection to the remote resource.
 InputStream getInputStream()
          Returns input stream to read server response from.
 T header(String name, String value)
          Sets an HTTP header - call before making a request.
 Map<String,List<String>> headers()
          Returns HTTP headers as sent by server.
 int responseCode()
          Returns HTTP response code.
 String responseMessage()
          Returns response message from server, such as "OK", or "Created", etc.
 String text()
          Fetches response content from server as String.
 String text(String encoding)
          Fetches response content from server as String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected final HttpURLConnection connection

url

protected final String url
Constructor Detail

Request

public Request(String url,
               int connectTimeout,
               int readTimeout)
Method Detail

header

public T header(String name,
                String value)
Sets an HTTP header - call before making a request.

Parameters:
name - header name
value - header value.
Returns:
self.

getInputStream

public InputStream getInputStream()
Returns input stream to read server response from.

Returns:
input stream to read server response from.

headers

public Map<String,List<String>> headers()
Returns HTTP headers as sent by server.

Returns:
HTTP headers as sent by server.

responseCode

public int responseCode()
Returns HTTP response code.

Returns:
HTTP response code.

responseMessage

public String responseMessage()
Returns response message from server, such as "OK", or "Created", etc.

Returns:
response message from server, such as "OK", or "Created", etc.

bytes

public byte[] bytes()
Fetches response content from server as bytes.

Returns:
response content from server as bytes.

text

public String text()
Fetches response content from server as String.

Returns:
response content from server as String.

text

public String text(String encoding)
Fetches response content from server as String.

Parameters:
encoding - - name of supported charset to apply when reading data.
Returns:
response content from server as String.

dispose

public void dispose()
This method is already called from text() and bytes(), you do not have to call it if you use those methods.

However, if you use getInputStream(), call this method in those cases when you think you did not read entire content from the stream.

This method clears all remaining data in connections after reading a response. This will help keep-alive work smoothly.


connect

protected T connect()

doConnect

protected abstract T doConnect()
Makes a connection to the remote resource.

Returns:
self.

basic

public T basic(String user,
               String password)
Sets a user and password for basic authentication.

Parameters:
user - user.
password - password.
Returns:
self.


Copyright © 2015 JavaLite. All rights reserved.