Java URL Post postUrl(URL url, String data)

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

Description

post Url

License

Open Source License

Declaration

public static String postUrl(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 postUrl(URL url, String data) throws IOException {
        OutputStreamWriter wr = null;
        BufferedReader rd = null;
        try {/*  w w  w.  ja  v  a 2 s.co  m*/
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the response
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = rd.readLine()) != null) {
                sb.append(line).append("\n");
            }
            return sb.toString();
        } finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    // NOP
                }
            }
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    // NOP
                }
            }
        }
    }
}

Related

  1. postData(String urlStr, String data)
  2. postData(URL base, String urlAddress, Map data)
  3. postForString(URL url, String data)
  4. postRequest(URLConnection conn, Map nameValuePairs)
  5. postToUrl(String page)
  6. writePostRequest(URLConnection connection, String postRequestBody, String contentType)
  7. xmlPost(String urlStr, String xmlInfo)
  8. writeString(URLConnection connection, String string)