Java HTTP Request sendGet(String url)

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

Description

send Get

License

Open Source License

Declaration

public static String sendGet(String url) throws Exception 

Method Source Code

//package com.java2s;
/*// w w  w.ja v a 2  s .co m
 * @(#)CommunicationHelper.java
 *
 * Copyright 2013 openmolecules.org, Inc. All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains the property
 * of openmolecules.org.  The intellectual and technical concepts contained
 * herein are proprietary to openmolecules.org.
 * Actelion Pharmaceuticals Ltd. is granted a non-exclusive, non-transferable
 * and timely unlimited usage license.
 *
 * @author Thomas Sander
 */

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    private static final String USER_AGENT = "Mozilla/5.0";

    public static String sendGet(String url) throws Exception {
        HttpURLConnection con = (HttpURLConnection) new URL(url)
                .openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        //      int responseCode = con.getResponseCode();   // 200 or 401

        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null)
            response.append(inputLine);

        in.close();

        return response.toString();
    }
}

Related

  1. request(String url, int timeout, String method, Map header)
  2. request(String url, Map cookies, Map parameters)
  3. requestData(String url)
  4. requestDataFromUrl(URL url, byte[] tosend, String userAgent)
  5. saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
  6. sendGet(String url, String param)
  7. sendGetRequest(String requestURL)
  8. sendGetRequest(String url, String cookies)