Here you can find the source of httpPost(String urlToRead, String post)
Parameter | Description |
---|---|
urlToRead | a parameter |
post | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static String httpPost(String urlToRead, String post) throws Exception
//package com.java2s; //License from project: Open Source License import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.Proxy; import java.net.URL; public class Main { /**/*from w w w . j a v a2 s . c om*/ * Performs synchronous HTTP POST request with raw data, returns the response. * @param urlToRead * @param post * @return * @throws Exception */ public static String httpPost(String urlToRead, String post) throws Exception { return httpPost(urlToRead, post, Proxy.NO_PROXY); } public static String httpPost(String urlToRead, String post, Proxy proxy) throws Exception { StringBuilder result = new StringBuilder(); URL url = new URL(urlToRead); HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); conn.setRequestMethod("POST"); conn.setDoOutput(true); final OutputStream os = conn.getOutputStream(); final BufferedOutputStream bos = new BufferedOutputStream(os); bos.write(post.getBytes("UTF-8")); bos.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); } }