editor.util.URLUtil.java Source code

Java tutorial

Introduction

Here is the source code for editor.util.URLUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package editor.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.springframework.stereotype.Component;

/**
 *
 * @author yoga1290
 */
@Component
public class URLUtil {

    public static String readText(String uri) {
        String res = "";
        try {
            java.net.URL url = new java.net.URL(uri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream in = connection.getInputStream();
            byte buff[] = new byte[200];
            int ch;
            while ((ch = in.read(buff)) != -1)
                res += new String(buff, 0, ch);
            in.close();

            return res;
        } catch (Exception e) {
            return e.getLocalizedMessage();
        }
    }

    public static String sendPost(String url, String data) throws Exception {

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }
}