Java HTTP Post postHTTPQuery(String url, String urlParameters)

Here you can find the source of postHTTPQuery(String url, String urlParameters)

Description

post HTTP Query

License

Educational Community License

Declaration

public static String postHTTPQuery(String url, String urlParameters) throws Exception 

Method Source Code


//package com.java2s;
/* =======================================================
   Copyright 2014 - ePortfolium - Licensed under the
   Educational Community 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//from   www .j  ava 2  s .c om
    
   http://www.osedu.org/licenses/ECL-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.BufferedReader;
import java.io.DataOutputStream;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    public static String postHTTPQuery(String url, String urlParameters) throws Exception {

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        return response.toString();

    }
}

Related

  1. post(URL url, String content)
  2. post(URL url, String contentType, byte[] data)
  3. postCall(JsonObject json, String url)
  4. postData(Reader data, URL endpoint, Writer output)
  5. postForm(String url, Map params)
  6. postJSON(JSONObject job, HttpURLConnection conn)
  7. postRaw(URL url, String params)
  8. postURL(final URL url, final String request)
  9. put(String url, String content)