Java HTTP Request sendGetRequest(String requestURL)

Here you can find the source of sendGetRequest(String requestURL)

Description

Makes an HTTP request using GET method to the specified URL.

License

Open Source License

Parameter

Parameter Description
requestURL the URL of the remote server

Exception

Parameter Description
IOException thrown if any I/O error occurred

Return

An HttpURLConnection object

Declaration

public static HttpURLConnection sendGetRequest(String requestURL) throws IOException 

Method Source Code

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

import java.io.IOException;

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

public class Main {
    /**/*from w  w w  .  j  a va 2 s  . c  o  m*/
     * Represents an HTTP connection
     */
    private static HttpURLConnection httpConn;

    /**
     * Makes an HTTP request using GET method to the specified URL.
     *
     * @param requestURL
     *            the URL of the remote server
     * @return An HttpURLConnection object
     * @throws IOException
     *             thrown if any I/O error occurred
     */
    public static HttpURLConnection sendGetRequest(String requestURL) throws IOException {
        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);

        httpConn.setDoInput(true); // true if we want to read server's response
        httpConn.setDoOutput(false); // false indicates this is a GET request

        return httpConn;
    }
}

Related

  1. requestData(String url)
  2. requestDataFromUrl(URL url, byte[] tosend, String userAgent)
  3. saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
  4. sendGet(String url)
  5. sendGet(String url, String param)
  6. sendGetRequest(String url, String cookies)