Java URL Read retrieve(URL url)

Here you can find the source of retrieve(URL url)

Description

retrieve

License

Open Source License

Declaration

public static String retrieve(URL url) throws IOException 

Method Source Code

//package com.java2s;
/****************************************************************************
 * Copyright (c) 2010 Giorgio Sironi. All rights reserved.
 * This program and the accompanying materials are made available under 
 * the terms of the Eclipse Public License v1.0 which accompanies this 
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 ****************************************************************************/

import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String retrieve(URL url) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");
        InputStream is = conn.getInputStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        for (int bytesRead = 0; (bytesRead = is.read(buffer)) != -1;) {
            output.write(buffer, 0, bytesRead);
        }//from  ww w.j  ava  2s  .c  om
        return output.toString();
    }
}

Related

  1. readURL(URLConnection connection, String charset)
  2. readUrlContent(URLConnection connection)
  3. readURLToString(String url)
  4. readUrlToString(String urlString)
  5. readVersion(URLConnection connection)
  6. retrieveResponseMessage(HttpURLConnection connection)