Java URL Post postForString(URL url, String data)

Here you can find the source of postForString(URL url, String data)

Description

post For String

License

Open Source License

Declaration

public static String postForString(URL url, String data) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String postForString(URL url, String data) throws IOException {
        String result = "";
        // System.out.println();
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);// ww  w  .  j  a v  a 2  s .co m
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            result += line + "\n";
        }
        wr.close();
        rd.close();

        return result;
    }
}

Related

  1. post(String url, String content)
  2. post(String urlstr, String[] params)
  3. post(String urlString, HashMap values)
  4. postData(String urlStr, String data)
  5. postData(URL base, String urlAddress, Map data)
  6. postRequest(URLConnection conn, Map nameValuePairs)
  7. postToUrl(String page)
  8. postUrl(URL url, String data)
  9. writePostRequest(URLConnection connection, String postRequestBody, String contentType)