Java HTTP Post doPost(URL url, String body, String... args)

Here you can find the source of doPost(URL url, String body, String... args)

Description

_more_

License

Apache License

Parameter

Parameter Description
url _more_
body _more_
args _more_

Exception

Parameter Description
Exception _more_

Return

_more_

Declaration

public static String doPost(URL url, String body, String... args) throws Exception 

Method Source Code

//package com.java2s;
/*/*  w w  w  .j a v  a 2 s.c  o m*/
* Copyright (c) 2008-2018 Geode Systems LLC
*
* 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.*;
import java.io.*;

import java.net.*;

public class Main {
    /**
     * _more_
     *
     * @param url _more_
     * @param body _more_
     * @param args _more_
     *
     * @return _more_
     *
     * @throws Exception _more_
     */
    public static String doPost(URL url, String body, String... args) throws Exception {
        return doHttpRequest("POST", url, body, args);
    }

    /**
     * _more_
     *
     * @param action _more_
     * @param url _more_
     * @param body _more_
     * @param args _more_
     *
     * @return _more_
     *
     * @throws Exception _more_
     */
    public static String doHttpRequest(String action, URL url, String body, String... args) throws Exception {
        //        URL url = new URL(request); 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod(action);
        connection.setRequestProperty("charset", "utf-8");
        for (int i = 0; i < args.length; i += 2) {
            //            System.err.println(args[i]+":" + args[i+1]);
            connection.setRequestProperty(args[i], args[i + 1]);
        }
        if (body != null) {
            connection.setRequestProperty("Content-Length", Integer.toString(body.length()));

            connection.getOutputStream().write(body.getBytes());
        }
        try {
            return readString(new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")));
        } catch (Exception exc) {
            System.err.println("Utils: error doing http request:" + action + "\nURL:" + url + "\nreturn code:"
                    + connection.getResponseCode() + "\nBody:" + body);
            System.err.println(readError(connection));
            System.err.println(connection.getHeaderFields());

            throw exc;
            //            System.err.println(connection.getContent());
        }
    }

    /**
     * _more_
     *
     * @param o _more_
     *
     * @return _more_
     */
    public static String toString(Object o) {
        if (o == null) {
            return "";
        }

        return o.toString();
    }

    /**
     * _more_
     *
     * @param input _more_
     *
     * @return _more_
     *
     * @throws Exception _more_
     */
    public static String readString(BufferedReader input) throws Exception {
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = input.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }

        return sb.toString();
    }

    /**
     * _more_
     *
     * @param conn _more_
     *
     * @return _more_
     */
    public static String readError(HttpURLConnection conn) {
        try {
            return readString(new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8")));

        } catch (Exception exc) {
            return "No error message";
        }
    }

    /**
     * _more_
     *
     * @param sb _more_
     * @param s _more_
     *
     * @return _more_
     */
    public static Appendable append(Appendable sb, String s) {
        try {
            sb.append(s);

            return sb;
        } catch (java.io.IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
}

Related

  1. doHttpPost(URL url, String requestBody)
  2. doPOST(HttpURLConnection conn, InputStream input)
  3. doPost(String targetURL, String urlParameters, String contentType)
  4. doPost(String theURL, Map nameValuePairs)
  5. executePost(String url, JSONObject data)
  6. executePostWithCredentials(String targetURL, String urlParameters, String userName, String password)
  7. getHttpPOSTConnection(String urlStr, String charSet, Map props, Map params)
  8. httpGet(final String httpurl, final Map... requestProperties)