Java HTTP Get httpGet(String urlToRead)

Here you can find the source of httpGet(String urlToRead)

Description

Performs synchronous HTTP GET request, returns the response.

License

Open Source License

Parameter

Parameter Description
urlToRead a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String httpGet(String urlToRead) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.InputStreamReader;

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

public class Main {
    /**/*from  w w w  .  j av  a  2 s. c o  m*/
     * Performs synchronous HTTP GET request, returns the response.
     *
     * @param urlToRead
     * @return
     * @throws Exception
     */
    public static String httpGet(String urlToRead) throws Exception {
        return httpGet(urlToRead, Proxy.NO_PROXY);
    }

    public static String httpGet(String urlToRead, Proxy proxy) throws Exception {
        StringBuilder result = new StringBuilder();
        URL url = new URL(urlToRead);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
        conn.setRequestMethod("GET");
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        rd.close();
        return result.toString();
    }
}

Related

  1. httpGet(String url)
  2. httpGet(String url, boolean logStdout)
  3. httpGet(String url, StringBuffer response)
  4. httpGet(String urlStr)
  5. httpGet(String urlStr)
  6. httpGetString(String url)
  7. readUrl(final String strUrl)
  8. readURL(final String textURL)
  9. readUrl(HttpURLConnection conn)