Java HTTP Post postData(Reader data, URL endpoint, Writer output)

Here you can find the source of postData(Reader data, URL endpoint, Writer output)

Description

Reads data from the data reader and posts it to a server via a POST request.

License

Apache License

Parameter

Parameter Description
data The data to send
endpoint The server's address
output Where to write the server's response

Exception

Parameter Description
IOException an exception

Declaration

public static void postData(Reader data, URL endpoint, Writer output) throws IOException 

Method Source Code

//package com.java2s;
/*//from w w  w  .j av a 2  s.  com
   This file is part of the LarKC platform 
   http://www.larkc.eu/
    
   Copyright 2010 LarKC project consortium
    
   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

public class Main {
    /**
     * Reads data from the data reader and posts it to a server via a POST
     * request.
     * 
     * @param data
     *            The data to send
     * @param endpoint
     *            The server's address
     * @param output
     *            Where to write the server's response
     * 
     * @throws IOException
     */
    public static void postData(Reader data, URL endpoint, Writer output) throws IOException {
        HttpURLConnection urlc = null;
        try {
            urlc = (HttpURLConnection) endpoint.openConnection();
            try {
                urlc.setRequestMethod("POST");
            } catch (ProtocolException e) {
                throw new RuntimeException("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
            }
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            urlc.setUseCaches(false);
            urlc.setAllowUserInteraction(false);
            urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

            OutputStream out = urlc.getOutputStream();

            try {
                Writer writer = new OutputStreamWriter(out, "UTF-8");
                pipe(data, writer);
                writer.close();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {
                    }
                }
            }

            InputStream in = urlc.getInputStream();
            try {
                Reader reader = new InputStreamReader(in);
                pipe(reader, output);
                reader.close();
            } finally {
                if (in != null)
                    in.close();
            }

        } finally {
            if (urlc != null)
                urlc.disconnect();
        }
    }

    /**
     * Pipes everything from the reader to the writer via a buffer
     */
    private static void pipe(Reader reader, Writer writer) throws IOException {
        char[] buf = new char[1024];
        int read = 0;
        while ((read = reader.read(buf)) >= 0) {
            writer.write(buf, 0, read);
        }
        writer.flush();
    }
}

Related

  1. Post(URL url, Map fields)
  2. post(URL url, Map values)
  3. post(URL url, String content)
  4. post(URL url, String contentType, byte[] data)
  5. postCall(JsonObject json, String url)
  6. postForm(String url, Map params)
  7. postHTTPQuery(String url, String urlParameters)
  8. postJSON(JSONObject job, HttpURLConnection conn)
  9. postRaw(URL url, String params)