Java Utililty Methods URL Post

List of utility methods to do URL Post

Description

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

Method

Stringpost(JSONObject json, String url)
POST mechanism
String query = json.toString();
URLConnection connection;
try {
    connection = new URL(url).openConnection();
    connection.setDoOutput(true); 
    connection.setRequestProperty("Accept-Charset", CHARSET);
    connection.setRequestProperty("Content-Type", "application/json;charset=" + CHARSET);
    try (OutputStream output = connection.getOutputStream()) {
...
Stringpost(String url, Map parameters)
post
URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("User-Agent",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
osw.write(parametersToWWWFormURLEncoded(parameters));
osw.flush();
...
Stringpost(String url, String content)
post
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 = "";
...
Stringpost(String url, String content)
post
URL u = new URL(url);
URLConnection c = u.openConnection();
c.setDoOutput(true);
writeToStream(c.getOutputStream(), content);
return getStreamContent(c.getInputStream());
Stringpost(String urlstr, String[] params)
post
try {
    StringBuffer param = new StringBuffer();
    for (int i = 0; i < params.length; i += 2) {
        param.append(URLEncoder.encode(params[i], "UTF-8") + "=" + URLEncoder.encode(params[i + 1], "UTF-8")
                + "&");
    URL url = new URL(urlstr);
    URLConnection conn = url.openConnection();
...
Stringpost(String urlString, HashMap values)
post
StringBuffer retval = new StringBuffer();
URL url = new URL(urlString);
StringBuffer data = new StringBuffer();
for (String key : values.keySet()) {
    data.append(URLEncoder.encode(key, "UTF-8") + "=");
    data.append(URLEncoder.encode(values.get(key), "UTF-8") + "&");
URLConnection conn = url.openConnection();
...
StringpostData(String urlStr, String data)
post Data
return postData(urlStr, data, null);
StringpostData(URL base, String urlAddress, Map data)
post Data
URL url = new URL(base, urlAddress);
return postData(url, data);
StringpostForString(URL url, String data)
post For String
String result = "";
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
...
voidpostRequest(URLConnection conn, Map nameValuePairs)
post Request
if (nameValuePairs != null) {
    if (useStdHeaders)
        for (int i = 0; i < stdReqHdr.length - 1; i++)
            conn.setRequestProperty(stdReqHdr[i], stdReqHdr[i + 1]);
    PrintWriter out = new PrintWriter(conn.getOutputStream());
    out.print(encodeParams(nameValuePairs));
    out.close();