java的post请求

public static String sendPost(String url,Map<String, String> packageParams){
//packageParams={channel=xajc, sign=5318a58f2125e17f164ba113c7574622}
PrintWriter out = null;
BufferedReader in = null;
HttpURLConnection conn = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Charset", "UTF-8");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
conn.setRequestMethod("POST");

// 获取URLConnection对象对应的输出流
//out = new PrintWriter(conn.getOutputStream());
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
StringBuffer params = new StringBuffer();
// 表单参数与get形式一样
for (Map.Entry<String,String> map : packageParams.entrySet()) {
params.append(map.getKey()).append("=").append(map.getValue()).append("&");
System.out.println(map);

}
params.deleteCharAt(params.lastIndexOf("&"));
// 发送请求参数
out.print(params.toString());
System.out.println("请求参数: " + params.toString());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
int resultCode=conn.getResponseCode();//400
if(HttpURLConnection.HTTP_OK==resultCode){
StringBuilder result = new StringBuilder();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}

return result.toString();
}
} catch (Exception e) {
e.fillInStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
return "";
}

public static String sendPostJSON(String url, String params){
PrintWriter out = null;
BufferedReader in = null;
HttpURLConnection conn = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Charset", "UTF-8");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(5000);
conn.setReadTimeout(10000);
conn.setRequestMethod("POST");

// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
// 发送请求参数
out.print(params.toString());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
int resultCode=conn.getResponseCode();//400
if(HttpURLConnection.HTTP_OK==resultCode){
StringBuilder result = new StringBuilder();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}

return result.toString();
}
} catch (Exception e) {
e.fillInStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
return "";
}

posted @ 2019-04-22 17:27  生之等待  阅读(1417)  评论(0编辑  收藏  举报