Java Utililty Methods HTTP Post

List of utility methods to do HTTP Post

Description

The list of methods to do HTTP Post are organized into topic(s).

Method

StringsendPost(String link, String urlParameters)
send Post
String url = baseUrl + link;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api"));
con.setRequestProperty("Content-Type", "application/json;charset=utf-8");
con.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF8");
...
intsendPost(String url, String data)
send Post
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(data);
wr.flush();
...
StringsendPost(String url, String urlParameters)
send Post
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
...
StringsendPost(String webURL, HashMap postOptions)
send Post
URL obj = new URL(webURL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "";
for (String s : postOptions.keySet()) {
    urlParameters = urlParameters
...
voidsendPostRequest(Reader data, URL endpoint, Writer output)
Reads data from the data reader and posts it to a server via POST request.
HttpURLConnection urlConnection = null;
try {
    urlConnection = (HttpURLConnection) endpoint.openConnection();
    try {
        urlConnection.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setAllowUserInteraction(false);
    urlConnection.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
    OutputStream out = urlConnection.getOutputStream();
    try {
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        pipe(data, writer);
        writer.close();
    } catch (IOException e) {
        throw new Exception("IOException while posting data", e);
    } finally {
        if (out != null) {
            out.close();
    InputStream in = urlConnection.getInputStream();
    try {
        Reader reader = new InputStreamReader(in);
        pipe(reader, output);
        reader.close();
    } catch (IOException e) {
        throw new Exception("IOException while reading response", e);
    } finally {
        if (in != null) {
            in.close();
} catch (IOException e) {
    throw new Exception("Connection error (is server running at " + endpoint + " ?): " + e);
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
voidsendPostRequest(Reader data, URL endpoint, Writer output)
Reads data from the data reader and posts it to a server via POST request.
HttpURLConnection urlConnection = null;
try {
    urlConnection = (HttpURLConnection) endpoint.openConnection();
    try {
        urlConnection.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setAllowUserInteraction(false);
    urlConnection.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
    OutputStream out = urlConnection.getOutputStream();
    try {
        Writer writer = new OutputStreamWriter(out, "UTF-8");
        pipe(data, writer);
        writer.close();
    } catch (IOException e) {
        throw new Exception("IOException while posting data: " + e.getMessage(), e);
    } finally {
        if (out != null) {
            out.close();
    InputStream in = urlConnection.getInputStream();
    try {
        Reader reader = new InputStreamReader(in);
        pipe(reader, output);
        reader.close();
    } catch (IOException e) {
        throw new Exception("IOException while reading response: " + e.getMessage(), e);
    } finally {
        if (in != null) {
            in.close();
} catch (IOException e) {
    throw new Exception("Connection error (is server running at " + endpoint + " ?): " + e.getMessage(), e);
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
HttpURLConnectionsendPostRequest(String requestURL, Map params)
Makes an HTTP request using POST method to the specified URL.
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoInput(true); 
StringBuffer requestParams = new StringBuffer();
if (params != null && params.size() > 0) {
    httpConn.setDoOutput(true); 
    Iterator<String> paramIterator = params.keySet().iterator();
...
ListURLPost(String strUrl, Map map)
URL Post
String content = "";
content = getUrl(map);
String totalURL = null;
if (strUrl.indexOf("?") == -1) {
    totalURL = strUrl + "?" + content;
} else {
    totalURL = strUrl + "&" + content;
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setAllowUserInteraction(false);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
bout.write(content);
bout.flush();
bout.close();
BufferedReader bin = new BufferedReader(new InputStreamReader(con.getInputStream()), 1048576);
List result = new ArrayList();
for (;;) {
    String line = bin.readLine();
    if (line == null) {
        break;
    result.add(line);
return result;
voidwriteBody(HttpURLConnection connection, String body)
write Body
OutputStream stream = connection.getOutputStream();
stream.write(body.getBytes());
stream.flush();
stream.close();
voidwriteContent(HttpURLConnection urlConn, String content)
write Content
OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
out.write(content);
out.close();