Java URL Connection doPost(String url, String params, String charset)

Here you can find the source of doPost(String url, String params, String charset)

Description

doPost

License

Open Source License

Parameter

Parameter Description
url a parameter
params a parameter

Declaration

public static String doPost(String url, String params, String charset) 

Method Source Code

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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**/*from  w ww .j  av a  2 s  .c  o m*/
     * doPost
     * @param url
     * @param params
     * @return
     */
    public static String doPost(String url, String params, String charset) {
        URL reqUrl = null;
        URLConnection conn = null;
        OutputStreamWriter out = null;
        BufferedWriter bw = null;
        OutputStream os = null;
        BufferedReader in = null;
        StringBuffer result = new StringBuffer();
        try {
            reqUrl = new URL(url);
            conn = reqUrl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //         conn.setRequestProperty("accept-charset", charset);

            //         out = new OutputStreamWriter(conn.getOutputStream(), charset);
            //         out.write(params);
            //         out.flush();

            //         os = conn.getOutputStream();
            //         byte[] paramsbyte = params.getBytes(charset);
            //         os.write(paramsbyte);
            //         os.flush();

            //         bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), charset));
            bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
            bw.write(params);
            bw.flush();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
            String line = null;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                out = null;
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                in = null;
            }
        }
        return result.toString();
    }
}

Related

  1. createURLConnection(String url)
  2. createWebStartDirectory(String name, String jarUrl)
  3. doGet(String targetURL)
  4. doGet(String url, String charset)
  5. doPost(final URL url, final Map parameters, final boolean encode)
  6. findClasses(URL resource, String packageName, boolean annotated)
  7. findClasspathUrls(ClassLoader classLoader)
  8. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
  9. getAsStream(URL url)