Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 chatter-bot-api
 Copyright (C) 2011 pierredavidbelanger@gmail.com
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;

import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import java.util.List;
import java.util.Map;

public class Main {
    public static String request(String url, Map<String, String> cookies, Map<String, String> parameters)
            throws Exception {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
        if (cookies != null && !cookies.isEmpty()) {
            StringBuilder cookieHeader = new StringBuilder();
            for (String cookie : cookies.values()) {
                if (cookieHeader.length() > 0) {
                    cookieHeader.append(";");
                }
                cookieHeader.append(cookie);
            }
            connection.setRequestProperty("Cookie", cookieHeader.toString());
        }
        connection.setDoInput(true);
        if (parameters != null && !parameters.isEmpty()) {
            connection.setDoOutput(true);
            OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
            osw.write(parametersToWWWFormURLEncoded(parameters));
            osw.flush();
            osw.close();
        }
        if (cookies != null) {
            for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
                if (headerEntry != null && headerEntry.getKey() != null
                        && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) {
                    for (String header : headerEntry.getValue()) {
                        for (HttpCookie httpCookie : HttpCookie.parse(header)) {
                            cookies.put(httpCookie.getName(), httpCookie.toString());
                        }
                    }
                }
            }
        }
        Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringWriter w = new StringWriter();
        char[] buffer = new char[1024];
        int n = 0;
        while ((n = r.read(buffer)) != -1) {
            w.write(buffer, 0, n);
        }
        r.close();
        return w.toString();
    }

    public static String parametersToWWWFormURLEncoded(Map<String, String> parameters) throws Exception {
        StringBuilder s = new StringBuilder();
        for (Map.Entry<String, String> parameter : parameters.entrySet()) {
            if (s.length() > 0) {
                s.append("&");
            }
            s.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
            s.append("=");
            s.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
        }
        return s.toString();
    }
}