Java HTTP Get httpGet(String url, StringBuffer response)

Here you can find the source of httpGet(String url, StringBuffer response)

Description

http Get

License

Open Source License

Declaration

public static int httpGet(String url, StringBuffer response) throws IOException 

Method Source Code


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

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 int httpGet(String url, StringBuffer response) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add request header
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        //Debug.out.info("Response Code: "+responseCode);

        if (response != null && responseCode == 200 /*OK*/) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }//  w  ww.  ja v  a2s. co m
            }
        }

        con.disconnect();

        //Logger.DebugTN("Response Data: "+response.toString());
        return responseCode;
    }
}

Related

  1. getHttpURLConnectionByGET(String resourceURL)
  2. getJsonFromUrl(String url)
  3. httpGet(String httpUrl)
  4. httpGet(String url)
  5. httpGet(String url, boolean logStdout)
  6. httpGet(String urlStr)
  7. httpGet(String urlStr)
  8. httpGet(String urlToRead)
  9. httpGetString(String url)