fetch HTTP Data - Java Network

Java examples for Network:Http

Description

fetch HTTP Data

Demo Code


import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.DataInputStream;
import java.util.List;
import java.util.Collections;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
    public static String charset = java.nio.charset.StandardCharsets.UTF_8
            .name();/*from  ww  w .  j  a va2s  .c o m*/
    private static String USER_AGENT = "Mozilla/5.0";
    public static String workingDirectory;
    /**
     * 
     * @param URL
     * @param query
     * @return
     * @throws IOException 
     */
    public static String fetchHTTPData(String URL, String query) {
        String response = "";
        int responseCode = 0;
        try {
            HttpURLConnection httpConn = (HttpURLConnection) new URL(URL
                    + "?" + query).openConnection();
            httpConn.setDoOutput(true); // Triggers POST.

            httpConn.setRequestProperty("Accept-Charset", charset);
            httpConn.setRequestProperty("User-Agent", USER_AGENT);
            responseCode = httpConn.getResponseCode();
            if (responseCode == 200) { //OK
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(httpConn.getInputStream(),
                                "UTF-8"));
                String inputLine;
                StringBuffer responseBuffer = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    responseBuffer.append(inputLine);
                }
                in.close();
                response = responseBuffer.toString();
            }
        } catch (Exception ex) {
            HelperFunctions
                    .writeInformationIntoFile("Exception while fetching HTTP from URL:"
                            + URL
                            + "?"
                            + query
                            + "\r\nError stackTrace :"
                            + ex.getMessage());
        }
        return response;
    }
    /**
     * Write the information into the log file
     */
    public static void writeInformationIntoFile(String text) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyy_MM_dd_hh_mm_ss");
        try {
            String fileName = HelperFunctions.workingDirectory
                    + "//log.txt";
            Writer out = new OutputStreamWriter(new FileOutputStream(
                    fileName, true), "UTF-8");
            try {
                out.write("\r\n" + "[ "
                        + simpleDateFormat.format(new Date()).toString()
                        + " ] : " + text);
            } finally {
                out.close();
            }

        } catch (Exception ex) {
        }
    }
}

Related Tutorials