Java URL Read getHttpResponse(String urlStr)

Here you can find the source of getHttpResponse(String urlStr)

Description

get Http Response

License

Open Source License

Declaration

public static String getHttpResponse(String urlStr) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  ww .  j a  v a2  s .  c o  m*/
 * Copyright (C) 2015 Weigandt Consulting
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static String getHttpResponse(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setConnectTimeout(5000);
        //conn.setReadTimeout(20000);

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        StringBuilder outputBuilder = new StringBuilder();
        String output;
        while ((output = br.readLine()) != null) {
            outputBuilder.append(output);
        }
        conn.disconnect();
        return outputBuilder.toString();
    }
}

Related

  1. getHtml(String urlString)
  2. getHTML(String urlToRead)
  3. getHTML(String urlToRead)
  4. getHttpResponse(HttpURLConnection conn, int expectedReturnCode)
  5. getHTTPResponse(String url)
  6. getLastModified(HttpURLConnection conn)
  7. getLastModified(HttpURLConnection connection)
  8. getRemoteFileLength(String url)
  9. getRemoteFileSize(URL url)