Java HTTP Request getOutputFromUrlConnection(String stringUrl, String requestProperty)

Here you can find the source of getOutputFromUrlConnection(String stringUrl, String requestProperty)

Description

get Output From Url Connection

License

Apache License

Declaration

public static String getOutputFromUrlConnection(String stringUrl,
            String requestProperty) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static String getOutputFromUrlConnection(String stringUrl,
            String requestProperty) {

        String outputString = null;

        try {/*w  w w . j  ava2  s  . c o m*/
            URL url = new URL(stringUrl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", requestProperty);
            conn.connect();

            if (conn.getResponseCode() != 200) {
                return "0";
            }

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

            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            outputString = sb.toString();

            br.close();
            conn.disconnect();

        } catch (MalformedURLException malformedException) {
            malformedException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        return outputString;

    }
}

Related

  1. addRequestProperties(final Map requestProperties, final HttpURLConnection connection)
  2. doHttpRequest(String action, URL url, String body, String... args)
  3. getContent(String requestUrl)
  4. getDataFromRequestViaPost(String request, String urlParameters)
  5. getRequest(String urlString)
  6. getRequest(URL url, int timeout)
  7. getRequestContent(String urlText)
  8. makeUrlRequest(String surl, String data, String method)