Java HTTP Get doGet(URL url, String... args)

Here you can find the source of doGet(URL url, String... args)

Description

_more_

License

Apache License

Parameter

Parameter Description
url _more_
args _more_

Exception

Parameter Description
Exception _more_

Return

_more_

Declaration

public static String doGet(URL url, String... args) throws Exception 

Method Source Code

//package com.java2s;
/*//  ww  w .j  av a  2s . c o m
* Copyright (c) 2008-2018 Geode Systems LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*     http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.net.*;

public class Main {
    /**
     * _more_
     *
     * @param url _more_
     * @param args _more_
     *
     * @return _more_
     *
     * @throws Exception _more_
     */
    public static String doGet(URL url, String... args) throws Exception {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //        connection.setDoOutput(true);
        //        connection.setDoInput(true);
        //        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        //        connection.setRequestProperty("charset", "utf-8");
        for (int i = 0; i < args.length; i += 2) {
            //System.err.println(args[i]+":" + args[i+1]);
            connection.setRequestProperty(args[i], args[i + 1]);
        }
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }

            return sb.toString();
        } catch (Exception exc) {
            System.err.println("Error:" + connection.getResponseCode());
            System.err.println(connection.getHeaderFields());

            throw exc;
            //            System.err.println(connection.getContent());
        }
    }

    /**
     * _more_
     *
     * @param sb _more_
     * @param s _more_
     *
     * @return _more_
     */
    public static Appendable append(Appendable sb, String s) {
        try {
            sb.append(s);

            return sb;
        } catch (java.io.IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    /**
     * _more_
     *
     * @param o _more_
     *
     * @return _more_
     */
    public static String toString(Object o) {
        if (o == null) {
            return "";
        }

        return o.toString();
    }
}

Related

  1. doGet(String theURL, Map nameValuePairs)
  2. doGet(String url)
  3. doGet(String url, HashMap params)
  4. doGet(String urlStr)
  5. doGet(String urlString)
  6. doGetRequest(String urlStr)
  7. executeGet(String targetURL)
  8. getHttpURLConnectionByGET(String resourceURL)
  9. getJsonFromUrl(String url)