Java Utililty Methods URL Query

List of utility methods to do URL Query

Description

The list of methods to do URL Query are organized into topic(s).

Method

intquery(URL host, String endpoint, String customer, String name, OutputStream out)
query
String f1 = (customer == null) ? null : "customer=" + customer;
String f2 = (name == null) ? null : "name=" + name;
String filter = ((f1 == null) ? "?" : "?" + f1 + "&") + ((f2 == null) ? "" : f2);
URL url = new URL(host, endpoint + filter);
int responseCode;
HttpURLConnection connection = null;
InputStream input = null;
try {
...
StringqueryEndpoint(String url)
Query an endpoint.
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection conn = null;
BufferedReader ins;
StringBuilder outs = new StringBuilder();
char[] buffer = new char[1024];
String text = null;
int tmpi;
try {
...
StringappendQueryParams(String url, Map params)
Append query parameters to given url
String fullUrl = new String(url);
if (params != null) {
    boolean first = (fullUrl.indexOf('?') == -1);
    for (String param : params.keySet()) {
        if (first) {
            fullUrl += '?';
            first = false;
        } else {
...
StringgetHTTPQuery(String urlToRead)
get HTTP Query
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
    url = new URL(urlToRead);
    conn = (HttpURLConnection) url.openConnection();
...
MapgetQueryParams(String url)
Retrieve the query parameters from given url
Map<String, String> params = new HashMap<String, String>();
int start = url.indexOf('?');
while (start != -1) {
    int equals = url.indexOf('=', start);
    String param = "";
    if (equals != -1) {
        param = url.substring(start + 1, equals);
    } else {
...
StringqueryHtml(String url)
query Html
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setInstanceFollowRedirects(true);
int code = connection.getResponseCode();
if (code == 200) {
    InputStream in = connection.getInputStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
...
Listsearch(String query, int numberOfUrls)
search
List<String> listOfURLs = new ArrayList<String>();
String APIkey = "AIzaSyDNZ6K2wI2z2-j1tTSaZrpgUQJpOV2DRv8";
query = URLEncoder.encode(query, "UTF-8");
String stringURL = "https://www.googleapis.com/customsearch/v1?key=" + APIkey
        + "&cx=013036536707430787589:_pqjad5hr1a&q=" + query + "&alt=json";
URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
...