Java URL Connection doGet(String url, String charset)

Here you can find the source of doGet(String url, String charset)

Description

doGet

License

Open Source License

Parameter

Parameter Description
url a parameter

Declaration

public static String doGet(String url, String charset) 

Method Source Code

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

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**/*  ww  w  .  j a v a 2s. c  om*/
     * doGet
     * @param url
     * @return
     */
    public static String doGet(String url, String charset) {
        URL reqUrl = null;
        URLConnection conn = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        try {
            reqUrl = new URL(url);
            conn = reqUrl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setDoInput(true);
            conn.setDoOutput(false);

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }

            //         InputStream is = conn.getInputStream();
            //         byte[] buf = new byte[1024];
            //         int i = -1;
            //         while((i=is.read(buf))!=-1) {
            //            result.append(new String(buf, 0, i));
            //         }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
        }
        return result.toString();
    }
}

Related

  1. createImgUrlConnection(String url)
  2. createURLClassLoader(String dirPath)
  3. createURLConnection(String url)
  4. createWebStartDirectory(String name, String jarUrl)
  5. doGet(String targetURL)
  6. doPost(final URL url, final Map parameters, final boolean encode)
  7. doPost(String url, String params, String charset)
  8. findClasses(URL resource, String packageName, boolean annotated)
  9. findClasspathUrls(ClassLoader classLoader)