Java URL Query queryEndpoint(String url)

Here you can find the source of queryEndpoint(String url)

Description

Query an endpoint.

License

Open Source License

Parameter

Parameter Description
url the URL of the endpoint

Return

the text obtained from the endpoint

Declaration

public static String queryEndpoint(String url) 

Method Source Code

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

import java.io.*;
import java.net.*;

public class Main {
    /**/*w ww  .  j a v a 2s .  c  om*/
     * Query an endpoint.
     * @param url the URL of the endpoint
     * @return the text obtained from the endpoint
     */
    public static String queryEndpoint(String url) {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection conn = null;
        BufferedReader ins;
        StringBuilder outs = new StringBuilder();
        char[] buffer = new char[1024];
        String text = null;
        int tmpi;

        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestProperty("Accept", "text/plain");
            conn.connect();
            ins = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));

            do {
                tmpi = ins.read(buffer, 0, buffer.length);
                if (tmpi > 0)
                    outs.append(buffer, 0, tmpi);
            } while (tmpi >= 0);

            text = outs.toString();
            conn.disconnect();
        } catch (Exception e) {
            //e.printStackTrace();
        } finally {
            if (conn != null)
                conn.disconnect();
        }

        return text;
    }
}

Related

  1. appendQueryParams(String url, Map params)
  2. getHTTPQuery(String urlToRead)
  3. getQueryParams(String url)
  4. query(URL host, String endpoint, String customer, String name, OutputStream out)
  5. queryHtml(String url)
  6. search(String query, int numberOfUrls)