Java URL Connection sendGetRequest(String resourceUrl, String contentType)

Here you can find the source of sendGetRequest(String resourceUrl, String contentType)

Description

Send an HTTP GET request to the given url

License

Apache License

Parameter

Parameter Description
resourceUrl - The full URL of the resource
contentType Type of the content

Return

- The response from the end point

Declaration

public static String sendGetRequest(String resourceUrl, String contentType) 

Method Source Code


//package com.java2s;
/*// ww  w.  j  av  a2 s  .c om
   This file is part of the LarKC platform 
   http://www.larkc.eu/
    
   Copyright 2010 LarKC project consortium
    
   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.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, String> mCache = Collections.synchronizedMap(new HashMap<String, String>());

    /**
     * HTTP GET request with text/xml content type.
     * 
     * @param url
     *            The URL to the resource.
     * @return The returned content.
     */
    public static String sendGetRequest(String url) {
        return sendGetRequest(url, "text/xml");
    }

    /**
     * Send an HTTP GET request to the given url
     * 
     * @param resourceUrl
     *            - The full URL of the resource
     * @param contentType
     *            Type of the content
     * @return - The response from the end point
     */
    public static String sendGetRequest(String resourceUrl, String contentType) {
        String response = mCache.get(resourceUrl);
        if (response == null) {
            response = "";
            if (resourceUrl.startsWith("http://")) {
                // Send a GET request
                try {
                    // Send data
                    URL url = new URL(resourceUrl);
                    URLConnection conn = url.openConnection();
                    conn.setRequestProperty("Content-type", contentType + "; charset=UTF-8");

                    // Get the response
                    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuffer sb = new StringBuffer();
                    String line;
                    while ((line = rd.readLine()) != null) {
                        sb.append(line);
                    }
                    rd.close();
                    response = sb.toString();

                    mCache.put(resourceUrl, response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return response;
    }
}

Related

  1. resolveModuleEntriesFromJar(URL url, String _prefix)
  2. retrieveData(URL url)
  3. retrieveFromUrl(String url, boolean useCache, boolean deleteOnExit)
  4. retrieveHtml(URLConnection http)
  5. scanPackageByJar(List> classes, final String packageName, final URL jarUrl, final boolean recursive)
  6. sendGetRequest(String urlStr)
  7. sendPost(String url, String param)
  8. setTimeout(URLConnection conn, int milliseconds)
  9. setTimeouts(URLConnection connection)