Java URL Post post(String url, String content)

Here you can find the source of post(String url, String content)

Description

post

License

Open Source License

Declaration

public static String post(String url, String content) 

Method Source Code

//package com.java2s;
/*/*from w ww  .j  a  v a2 s  .  c  o m*/
 * Copyright (c) 2004-2013 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * https://www.lsts.pt/neptus/licence.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: 
 * 20??/??/??
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String post(String url, String content) {
        try {
            URLConnection conn = new URL(url).openConnection();
            conn.setDoOutput(true);

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
            bw.write(content);
            bw.close();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String result = "";
            String line = reader.readLine();
            while (line != null) {
                result = result.concat(line + "\n");
                line = reader.readLine();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "ERROR";

        }
    }
}

Related

  1. post(JSONObject json, String url)
  2. post(String url, Map parameters)
  3. post(String url, String content)
  4. post(String urlstr, String[] params)
  5. post(String urlString, HashMap values)
  6. postData(String urlStr, String data)
  7. postData(URL base, String urlAddress, Map data)