Java HTTP Get httpGet(String urlStr)

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

Description

http Get

License

Open Source License

Declaration

public static String httpGet(String urlStr) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String httpGet(String urlStr) throws IOException {

        URL url = new URL(urlStr);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() != 200)
            throw new IOException(conn.getResponseMessage());

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuilder sb = new StringBuilder();

        String line;/*from   ww w . j a  v a 2  s  . c  om*/
        while ((line = rd.readLine()) != null)
            sb.append(line);

        rd.close();

        conn.disconnect();

        return sb.toString();
    }
}

Related

  1. getJsonFromUrl(String url)
  2. httpGet(String httpUrl)
  3. httpGet(String url)
  4. httpGet(String url, boolean logStdout)
  5. httpGet(String url, StringBuffer response)
  6. httpGet(String urlStr)
  7. httpGet(String urlToRead)
  8. httpGetString(String url)
  9. readUrl(final String strUrl)