Java Utililty Methods HTTP Response

List of utility methods to do HTTP Response

Description

The list of methods to do HTTP Response are organized into topic(s).

Method

longgetResourceLastModified(URLConnection connection)
get Resource Last Modified
long modified;
if (connection instanceof JarURLConnection) {
    URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL();
    URLConnection jarFileConnection = jarFileUrl.openConnection();
    try {
        modified = jarFileConnection.getLastModified();
    } finally {
        try {
...
longgetResourceModifiedTime(URL url)
Retrieves the last modified time of a provided URL.
long lastModified = 0L;
try {
    if (url.getProtocol().equals(JAR_PROTOCOL)) {
        JarURLConnection jarConn = (JarURLConnection) url.openConnection();
        url = jarConn.getJarFileURL();
    if (url.getProtocol().equals(FILE_PROTOCOL)) {
        File file;
...
voidgetResponse(HttpURLConnection httpConn, StringBuilder responseContent, StringBuilder responseCode, HashMap headers)
Get the response content and headers as string
String s = getResponseContent(httpConn);
responseContent.setLength(0);
responseContent.append(s);
s = getResponseHeaders(httpConn, headers);
responseCode.setLength(0);
responseCode.append(s);
return;
StringgetResponse(URL request)
get Response
URLConnection yc = request.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
StringBuilder sb = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
    sb.append(inputLine);
in.close();
...
StringgetResponse(URL url)
Returns the content of the response body for the given url
StringBuffer response = new StringBuffer();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(GET);
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
    String sCurrentLine;
    while ((sCurrentLine = br.readLine()) != null) {
        response.append(sCurrentLine);
return response.toString();
StringgetResponseAsString(HttpURLConnection conn)
get Response As String
String charset = getResponseCharset(conn.getContentType());
InputStream es = conn.getErrorStream();
if (es == null) {
    return getStreamAsString(conn.getInputStream(), charset);
} else {
    String msg = getStreamAsString(es, charset);
    if (isEmpty(msg)) {
        throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
...
StringgetResponseBody(HttpURLConnection conn)
get Response Body
BufferedReader br = null;
StringBuilder body = null;
String line = "";
try {
    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    body = new StringBuilder();
    while ((line = br.readLine()) != null)
        body.append(line);
...
StringgetResponseContent(HttpURLConnection connection)
get Response Content
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
try {
    while ((line = reader.readLine()) != null) {
        response.append(line);
        response.append('\n');
...
StringgetResponseContent(HttpURLConnection httpConn)
Get the response (or error response) as a string
try {
    if (httpConn.getInputStream() == null) {
        return null;
    } else {
        StringBuilder sb = new StringBuilder();
        BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
        String str;
        while ((str = in.readLine()) != null) {
...
StringgetResponseContent(String url)
get Response Content
StringBuffer responseString = new StringBuffer();
URLConnection connection = new URL(url).openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String aux;
while ((aux = in.readLine()) != null) {
    responseString.append(aux);
in.close();
...