Java URL Read getURL(URL url, String params)

Here you can find the source of getURL(URL url, String params)

Description

Get a GET String response

License

Open Source License

Parameter

Parameter Description
url a parameter
params a parameter

Declaration

public static String getURL(URL url, String params) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**/*from ww w.  j a v a 2  s  .c  o  m*/
     * Get a GET String response
     * @param url
     * @param params
     * @return
     */
    public static String getURL(URL url, String params) {
        StringBuilder sb = new StringBuilder();

        String lineSepatator = null;
        try {
            lineSepatator = System.getProperty("line.separator");
        } catch (Exception e) {
            lineSepatator = "\n";
        }

        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setAllowUserInteraction(false);
            connection.setDoOutput(false);

            /*  String userpass = "anonymous" + ":" + "";
              String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
              connection.setRequestProperty ("Authorization", basicAuth);*/

            InputStream response = connection.getInputStream();
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(response));
            String sLine;

            while ((sLine = bufReader.readLine()) != null) {
                sb.append(sLine);
                sb.append(lineSepatator);
            }

            connection.disconnect();
        } catch (ConnectException ctx) {
            ctx.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}

Related

  1. getTextFromURL(final String url)
  2. getTextFromURL(URL url)
  3. getTextFromURL(URL url)
  4. getURL(final URL url)
  5. getURL(String url)
  6. getUrlContent(String url)
  7. getURLContent_old(final String uri, final StringBuffer content)
  8. getUrlContentWithRetries(String url, long timeoutMs, long retryDelayMs)
  9. getUrlFollowingRedirects(String possibleRedirectionUrl)