Here you can find the source of doPost(String url, String params, String charset)
Parameter | Description |
---|---|
url | a parameter |
params | a parameter |
public static String doPost(String url, String params, String charset)
//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(); } }