Java实现发送HTTP请求,包含GET、POST、POST(json格式参数)
package com.chenyi.quickstart.common;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
/**
* HTTP请求工具类
* GET、POST、POST(JSON格式参数)
*
* @author chenyi
* date: create on 2019/4/27
*/
public class HttpUtil {
/**
* 发送GET请求
*
* @param requestUrl 发送请求的URL
* @param params param1=value1¶m2=value2
* @return 远程资源响应结果 local-Error:本地请求错误
*/
public static String getRequest(String requestUrl, String params) {
requestUrl = requestUrl + "?" + params;
return HttpUtil.getGeneralUrl(requestUrl);
}
/**
* 发送 POST 请求 JSON格式
*
* @param requestUrl 请求地址
* @param jsonParams 请求参数 JSON格式
* @return 远程资源响应结果 local-Error:本地请求错误
*/
public static String postJson(String requestUrl, String jsonParams) {
// 字符集
String encoding = "utf-8";
//数据格式
String contentType = "application/json";
return HttpUtil.postGeneralUrl(requestUrl, contentType, jsonParams, encoding);
}
/**
* 发送POST请求
*
* @param requestUrl 请求URL地址
* @param params Map类型的参数
* @return 远程资源响应结果 local-Error:本地请求错误
*/
public static String postRequest(String requestUrl, Map<String, String> params) {
String contentType = "application/x-www-form-urlencoded";
String encoding = "utf-8";
String paramsStr = packParams(params);
return HttpUtil.postGeneralUrl(requestUrl, contentType, paramsStr, encoding);
}
/**
* 发送POST请求
*
* @param requestUrl 请求URL地址
* @param params param1=value1¶m2=value2
* @return 远程资源响应结果 local-Error:本地请求错误
*/
public static String postRequest(String requestUrl, String params) {
String contentType = "application/x-www-form-urlencoded";
String encoding = "utf-8";
return HttpUtil.postGeneralUrl(requestUrl, contentType, params, encoding);
}
/**
* 拼接参数 Http参数
*/
private static String packParams(Map<String, String> paramMap) {
//参数
StringBuilder params = new StringBuilder();
//遍历添加参数
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
params.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
return params.toString().substring(0, params.length() - 1);
}
/**
* 通用的GET请求
*
* @param generalUrl 发送请求的URL
* @return 远程资源响应结果
*/
private static String getGeneralUrl(String generalUrl) {
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("Connection", "Keep-Alive");
// 建立实际的连接
connection.connect();
inputStreamReader = new InputStreamReader(connection.getInputStream(), "utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder result = new StringBuilder();
String textLine;
while ((textLine = bufferedReader.readLine()) != null) {
result.append(textLine);
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭输出流、输入流
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "local-Error";
}
/**
* 通用的POST请求
*
* @param generalUrl 请求地址
* @param contentType 请求类型
* @param params 参数
* @param encoding 字符编码
* @return 远程资源响应结果 local-Error:本地请求错误
*/
private static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding) {
OutputStreamWriter out = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置请求方式为POST
connection.setRequestMethod("POST");
// 设置通用的请求属性
//POST请求的Content-Type 为: application/x-www-form-urlencoded
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
// 发送请求参数
out.write(params);
// flush输出流的缓冲
out.flush();
// 建立实际的连接
connection.connect();
inputStreamReader = new InputStreamReader(connection.getInputStream(), encoding);
bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder result = new StringBuilder();
String textLine;
while ((textLine = bufferedReader.readLine()) != null) {
result.append(textLine);
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭输出流、输入流
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "local-Error";
}
}