Skip to content

Instantly share code, notes, and snippets.

@jonnybest
Created December 6, 2013 15:12
Show Gist options
  • Save jonnybest/7826243 to your computer and use it in GitHub Desktop.
Save jonnybest/7826243 to your computer and use it in GitHub Desktop.

Revisions

  1. jonnybest created this gist Dec 6, 2013.
    36 changes: 36 additions & 0 deletions getRequest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    /** Fetches a website from the given URL.
    *
    * @param targetURL
    * @return the website as a string
    * @throws IOException
    */
    public static String executeGet(String targetURL) throws IOException {
    URL url;
    HttpURLConnection connection = null;

    // Create connection
    url = new URL(targetURL);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");

    // configure socket settings
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setReadTimeout(15000); // wait 15 seconds

    //System.out.println("connection prepared. now sending");

    // Send & Get Response
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
    response.append(line);
    response.append('\r');
    }

    connection.disconnect();
    rd.close();
    return response.toString();
    }