get Body from URL and header name and value - Android Network

Android examples for Network:HTTP Get

Description

get Body from URL and header name and value

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {

    public static byte[] getBody(String url, String headerName,
            String headerValue) {
        byte[] value = null;
        URL cURL = null;// w  w  w.j  av  a2s  . c  o  m
        URLConnection connection = null;
        InputStream is = null;
        try {
            cURL = new URL(url);
            connection = cURL.openConnection();
            // ????????
            connection.setDoOutput(true);
            connection.addRequestProperty(headerName, headerValue);
            connection.addRequestProperty("Connection", "keep-alive");
            connection
                    .addRequestProperty(
                            "User-Agent",
                            "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
            connection.setConnectTimeout(5 * 1000);
            connection.setReadTimeout(10 * 1000);
            connection.connect();
            int length = connection.getContentLength();
            is = connection.getInputStream();
            if (length > 0) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[2048];
                int ch;
                while ((ch = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, ch);
                    baos.flush();
                }
                baos.close();
                value = baos.toByteArray();
            }
        } catch (Exception e) {
            System.err.println("ERROR:" + e);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return value;
    }

    public static byte[] getBody(String url) {
        byte[] value = null;
        URL cURL = null;
        URLConnection connection = null;
        InputStream is = null;
        try {
            cURL = new URL(url);
            connection = cURL.openConnection();
            // ????????
            connection.setDoOutput(true);
            connection
                    .addRequestProperty(
                            "User-Agent",
                            "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4");
            connection.addRequestProperty("Connection", "keep-alive");
            connection.setConnectTimeout(10 * 1000);
            connection.setReadTimeout(10 * 1000);
            connection.connect();
            int length = connection.getContentLength();
            is = connection.getInputStream();
            if (length > 0) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[2048];
                int ch;
                while ((ch = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, ch);
                    baos.flush();
                }
                baos.close();
                value = baos.toByteArray();
            }
        } catch (Exception e) {
            System.err.println("ERROR:" + e);
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return value;
    }
}

Related Tutorials