Java HTTP Read getContentFromHttpGetBasicAuth(String urlString, String username, String password)

Here you can find the source of getContentFromHttpGetBasicAuth(String urlString, String username, String password)

Description

Will return empty string in case of errors.

License

Open Source License

Declaration

public static String getContentFromHttpGetBasicAuth(String urlString, String username, String password) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors://from  ww w.  ja va 2s . c o m
 *     Jochen Hiller
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**
     * Will return empty string in case of errors.
     */
    public static String getContentFromHttpGetBasicAuth(String urlString, String username, String password) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            String userpass = username + ":" + password;
            String basicAuth = "Basic " + base64encode(userpass.getBytes());
            con.setRequestProperty("Authorization", basicAuth);

            // TODO jhi some content is missing at the beginning
            InputStream is = con.getInputStream();
            int responseCode = con.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                System.err.println(
                        "TestUtils: getContentFromHttpGet for '" + urlString + "'failed with rc=" + responseCode);
                return "";
            }

            StringBuffer sbuf = new StringBuffer();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sbuf.append(line);
                sbuf.append('\n');
            }
            reader.close();
            String content = sbuf.toString();
            return content;
        } catch (IOException ex) {
            ex.printStackTrace();
            return "";
        }
    }

    /**
     * Base64 Encode an array of bytes
     * 
     * @see https://gist.github.com/EmilHernvall/953733
     */
    private static String base64encode(byte[] data) {
        char[] tbl = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', '+', '/' };

        StringBuilder buffer = new StringBuilder();
        int pad = 0;
        for (int i = 0; i < data.length; i += 3) {

            int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF;
            if (i + 1 < data.length) {
                b |= (data[i + 1] & 0xFF) << 8;
            } else {
                pad++;
            }
            if (i + 2 < data.length) {
                b |= (data[i + 2] & 0xFF);
            } else {
                pad++;
            }

            for (int j = 0; j < 4 - pad; j++) {
                int c = (b & 0xFC0000) >> 18;
                buffer.append(tbl[c]);
                b <<= 6;
            }
        }
        for (int j = 0; j < pad; j++) {
            buffer.append("=");
        }

        return buffer.toString();
    }
}

Related

  1. getContent(final HttpURLConnection con)
  2. getContent(final String urlString)
  3. getContent(String urlStr)
  4. getContentAsString(URL url)
  5. getContentEncoding(URL url)
  6. getContentFromURL(String sURL)
  7. getContentFromURL(String url)
  8. getContentLength(URL url)
  9. getContentLength(URLConnection con)