Java URL Post postData(URL base, String urlAddress, Map data)

Here you can find the source of postData(URL base, String urlAddress, Map data)

Description

post Data

License

Open Source License

Declaration

public static String postData(URL base, String urlAddress, Map data) throws MalformedURLException 

Method Source Code


//package com.java2s;
/* ===============================================================================
 *
 * Part of the InfoglueIDE Project //w  w w .j a  va2s  .  c  om
 *
 * ===============================================================================
 *
 * Copyright (C) Stefan Sik 2007
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2, as published by the
 * Free Software Foundation. See the file LICENSE.html for more information.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
 *
 * ===============================================================================
 */

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

public class Main {
    public static String postData(URL base, String urlAddress, Map data) throws MalformedURLException {
        URL url = new URL(base, urlAddress);
        return postData(url, data);
    }

    public static String postData(URL url, Map data) {
        StringBuilder returnData = new StringBuilder();
        try {
            URLConnection urlConn;
            DataOutputStream printout;
            DataInputStream input;
            urlConn = url.openConnection();
            urlConn.setDoInput(true);
            urlConn.setDoOutput(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            printout = new DataOutputStream(urlConn.getOutputStream());

            String content = "";
            String separator = "";
            for (Iterator i = data.keySet().iterator(); i.hasNext();) {
                String key = (String) i.next();
                String value = (String) data.get(key);
                content += separator + key + "=" + URLEncoder.encode(value, "UTF-8");
                separator = "&";
            }

            printout.writeBytes(content);
            printout.flush();
            printout.close();

            // Get response data.
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String l = null;
            while ((l = reader.readLine()) != null) {
                returnData.append(l);
                returnData.append("\n");
            }
            /*
            input = new DataInputStream(urlConn.getInputStream());
            while (null != ((str = input.readLine())))
            {
            // System.out.println("RESPONSEDATA:" + str);
            }
            */

            reader.close();

        } catch (MalformedURLException me) {
            System.err.println("MalformedURLException: " + me);
        } catch (IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
        }
        return returnData.toString();

    }
}

Related

  1. post(String url, String content)
  2. post(String url, String content)
  3. post(String urlstr, String[] params)
  4. post(String urlString, HashMap values)
  5. postData(String urlStr, String data)
  6. postForString(URL url, String data)
  7. postRequest(URLConnection conn, Map nameValuePairs)
  8. postToUrl(String page)
  9. postUrl(URL url, String data)