Java URL Post postToUrl(String page)

Here you can find the source of postToUrl(String page)

Description

post To Url

License

Mozilla Public License

Declaration

public static boolean postToUrl(String page) 

Method Source Code


//package com.java2s;
/*/*from  w ww .j a v a  2  s .c  o  m*/
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is vox-mail.
 *
 * The Initial Developer of the Original Code is Voxeo Corporation.
 * Portions created by Voxeo are Copyright (C) 2000-2007.
 * All rights reserved.
 * 
 * Contributor(s):
 * ICOA Inc. <info@icoa.com> (http://icoa.com)
 */

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class Main {
    public static boolean postToUrl(String page) {
        URL url;
        URLConnection urlConn;
        DataOutputStream printout;
        DataInputStream input;
        try {
            url = new URL(page);
            // URL connection channel.
            urlConn = url.openConnection();
            // Let the run-time system (RTS) know that we want input.
            urlConn.setDoInput(true);
            // Let the RTS know that we want to do output.
            urlConn.setDoOutput(true);
            // No caching, we want the real thing.
            urlConn.setUseCaches(false);
            // Specify the content type.
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // Send POST output.
            printout = new DataOutputStream(urlConn.getOutputStream());
            String content = "name=" + URLEncoder.encode("test", "UTF-8") + "&email="
                    + URLEncoder.encode("test@email.com", "UTF-8");
            printout.writeBytes(content);
            printout.flush();
            printout.close();

            // Get response data.
            input = new DataInputStream(urlConn.getInputStream());
            String str;
            while (null != ((str = input.readLine()))) {
                System.out.println(str);
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. post(String urlString, HashMap values)
  2. postData(String urlStr, String data)
  3. postData(URL base, String urlAddress, Map data)
  4. postForString(URL url, String data)
  5. postRequest(URLConnection conn, Map nameValuePairs)
  6. postUrl(URL url, String data)
  7. writePostRequest(URLConnection connection, String postRequestBody, String contentType)
  8. xmlPost(String urlStr, String xmlInfo)
  9. writeString(URLConnection connection, String string)