Java HTTP Get readUrl(String url, String token)

Here you can find the source of readUrl(String url, String token)

Description

read Url

License

Apache License

Declaration

public static String readUrl(String url, String token) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String readUrl(String url, String token) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        if (token != null) {
            con.setRequestProperty("X-Auth-Token", token);
        }//from   w w  w .  j av a  2 s  . c  o  m
        //int responseCode = con.getResponseCode();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }
}

Related

  1. httpGet(String urlToRead)
  2. httpGetString(String url)
  3. readUrl(final String strUrl)
  4. readURL(final String textURL)
  5. readUrl(HttpURLConnection conn)
  6. readURL(String url, String type)
  7. readUrl(String urlAsString, int timeout)
  8. readURL(URL url)
  9. requestGetMethod(String url, String... params)