Java HTTP Post put(String url, String content)

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

Description

put

License

Open Source License

Declaration

public static String put(String url, String content) 

Method Source Code

//package com.java2s;
/*/*  w w w.j  a va2 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.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String put(String url, String content) {
        HttpURLConnection conn = null;

        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("PUT");

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

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

            return null;
        }
    }
}

Related

  1. postForm(String url, Map params)
  2. postHTTPQuery(String url, String urlParameters)
  3. postJSON(JSONObject job, HttpURLConnection conn)
  4. postRaw(URL url, String params)
  5. postURL(final URL url, final String request)
  6. put(URL host, String endpoint, String customer, String name, String version, InputStream in)
  7. putPOST(HttpURLConnection h, String query)
  8. requestPost(final String remoteUrl, final byte[] content)
  9. sendJson(URL url, String method, String data)