package xx.xx.xx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
/**
* 发送http请求工具
*
* @author Shi Zezhu
* @date 2016年10月24日 下午5:01:06
*/
public class ConnectionUtils {
/**
* 请求类型:GET
*/
public static final String HTTP_TYPE_GET = "GET";
/**
* 请求类型:POST
*/
public static final String HTTP_TYPE_POST = "POST";
/**
* 发送http请求
*
* @param urlString
* url地址
* @param method
* 请求类型
* @param parameters
* 请求参数
* @return JSONObject string
* @throws IOException
*/
private static String send(String urlString, String method, String data) throws IOException {
if (StringUtils.isBlank(urlString) || StringUtils.isBlank(method)) {
return null;
}
if (HTTP_TYPE_GET.equalsIgnoreCase(method) && StringUtils.isNotBlank(data)) {
urlString = urlString + "?" + data;
}
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);// 设置请求方法
connection.setDoInput(true);// 设置是否使用输入流
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(5 * 1000);
if (HTTP_TYPE_POST.equalsIgnoreCase(method) && StringUtils.isNotBlank(data)) {
connection.setDoOutput(true);// 设置是否使用输出流
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
out.write(data);
out.flush();
out.close();
}
InputStream is = null;
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK || connection.getResponseCode() == HttpURLConnection.HTTP_CREATED || connection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
is = connection.getInputStream();
} else {
is = connection.getErrorStream();
}
if (is == null) {
return "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
connection.disconnect();
return sb.toString();
}
public static String mapToParamStr(Map<String, String> parameters) {
String data = "";
if (parameters != null && parameters.size() > 0) {
int i = 0;
StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : parameters.entrySet()) {
if (i > 0) {
sb.append("&");
}
sb.append(entry.getKey()).append("=").append(entry.getValue());
i++;
}
data = sb.toString();
}
return data;
}
/**
* 发送get请求
*
* @param urlString
* @param parameters
* @return
* @throws IOException
*/
public static String sendGet(String url, Map<String, String> parameters) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_GET, mapToParamStr(parameters));
}
/**
* 发送get请求
*
* @param urlString
* @param paramStr
* @return
* @throws IOException
*/
public static String sendGet(String url, String paramStr) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_GET, paramStr);
}
/**
* 发送get请求
*
* @param urlString
* @return
* @throws IOException
*/
public static String sendGet(String url) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_GET, null);
}
/**
* 发送post请求
*
* @param urlString
* @param parameters
* @return
* @throws IOException
*/
public static String sendPost(String url, Map<String, String> parameters) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_POST, mapToParamStr(parameters));
}
/**
* 发送post请求
*
* @param urlString
* @param paramStr
* @return
* @throws IOException
*/
public static String sendPost(String url, String paramStr) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_POST, paramStr);
}
/**
* 发送post请求
*
* @param urlString
* @return
* @throws IOException
*/
public static String sendPost(String url) throws IOException {
if (StringUtils.isBlank(url)) {
return null;
}
return send(url, HTTP_TYPE_POST, null);
}
}
java 发送HTTP请求工具
最新推荐文章于 2024-07-06 14:33:59 发布