Java Utililty Methods HTTP Read

List of utility methods to do HTTP Read

Description

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

Method

longgetContentLength(URLConnection con)
get Content Length
long res = con.getContentLength();
if (res == -1) {
    try {
        String str = con.getHeaderField("content-length");
        if (str != null) {
            res = Long.parseLong(str);
    } catch (Throwable e) {
...
StringgetContents(String urlStr)
Invokes URL and get the contents returned.
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
...
StringgetContentTypeFromUrl(String urlname)
get Content Type From Url
URL url = new URL(urlname);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
    connection.setRequestMethod("HEAD");
    connection.connect();
    return connection.getContentType();
} finally {
    connection.disconnect();
...
StringgetContentTypeSpecified(URLConnection connection)
get Content Type Specified
return connection.getHeaderField(CONTENT_TYPE);
StringgetContentWithPost(String urls, Map pv)
get Content With Post
if (pv.isEmpty())
    return "";
OutputStreamWriter writer = null;
BufferedReader reader = null;
try {
    String body = "";
    for (Map.Entry<String, String> entry : pv.entrySet()) {
        body += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), "UTF-8") + "&";
...
StringgetData(String urlString)
get Data
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int i;
String incomingMessage = "";
while ((i = is.read()) != -1) {
    incomingMessage = incomingMessage + (char) i;
...
byte[]getHtmlByteArray(final String url)
get Html Byte Array
URL htmlUrl = null;
InputStream inStream = null;
try {
    htmlUrl = new URL(url);
    URLConnection connection = htmlUrl.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
...
StringgetHtmlContent(String url)
get Html Content
return getHtmlContent(getConnection(url));
InputStreamgetInputStream(HttpURLConnection conn)
get Input Stream
return getInputStream(conn, false); 
InputStreamgetInputStream(HttpURLConnection connection)
Gets the correct java.io.InputStream based on urlConnection encoding.
if (connection.getRequestMethod().equals("HEAD"))
    return null;
String encoding = connection.getContentEncoding();
if (encoding == null)
    return connection.getInputStream();
else if (encoding.equalsIgnoreCase("gzip"))
    return new GZIPInputStream(connection.getInputStream());
else if (encoding.equalsIgnoreCase("deflate"))
...