Java原生http请求post,需要的朋友可以参考下。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpUtils {
public static String sendPost(String uri, String charset) {
String result = null;
InputStream in = null;
try {
URL url = new URL(uri);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.connect();// 获取连接
in = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(
in, charset));
StringBuffer bs = new StringBuffer();
String line = null;
while ((line = buffer.readLine()) != null) {
bs.append(line);
}
result = bs.toString();
} catch (Exception e) {
System.out.println("[请求异常][地址:" + uri + "][错误信息:" + e.getMessage()
+ "]");
} finally {
try {
if (null != in)
in.close();
} catch (Exception e2) {
System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");
}
}
return result;
}
/**
*
* @param uri
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @param charset
* @return
*/
public static String sendPost(String uri, String param, String charset) {
String result = null;
PrintWriter out = null;
InputStream in = null;
try {
URL url = new URL(uri);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setDoInput(true);
urlcon.setDoOutput(true);
urlcon.setUseCaches(false);
urlcon.setRequestMethod("POST");
urlcon.connect();// 获取连接
out = new PrintWriter(urlcon.getOutputStream());
out.print(param);
out.flush();
in = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(
in, charset));
StringBuffer bs = new StringBuffer();
String line = null;
while ((line = buffer.readLine()) != null) {
bs.append(line);
}
result = bs.toString();
} catch (Exception e) {
System.out.println("[请求异常][地址:" + uri + "][参数:" + param + "][错误信息:"
+ e.getMessage() + "]");
} finally {
try {
if (null != in)
in.close();
if (null != out)
out.close();
} catch (Exception e2) {
System.out.println("[关闭流异常][错误信息:" + e2.getMessage() + "]");
}
}
return result;
}
/**
* Do GET request
* @param url
* @return
* @throws Exception
* @throws IOException
*/
public String doGet(String url) throws Exception {
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
//响应失败
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
try {
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} finally {
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer.toString();
}
}