使用HttpUrlConnection发送GET和POST请求的工具类:
废话不多说直接上代码:
package com.zoho.utils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HttpUrlConnectionUtils {
public static String get(String requestUrl) {
return get(requestUrl, new HashMap<String, String>(), new HashMap<String, String>());
}
public static String get(String requestUrl, Map<String, String> params) {
return get(requestUrl, params, new HashMap<String, String>());
}
public static String get(String requestUrl, Map<String, String> params, Map<String, String> headers) {
if (requestUrl == null || "".equals(requestUrl)) {
throw new RuntimeException("URL不正确!");
}
String finalUrl = requestUrl;
if (!params.isEmpty()) {
String urlParam = "";
if (!params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String s = key + "=" + value + "&";
urlParam += s;
}
}
finalUrl = requestUrl + "?" + urlParam.substring(0, urlParam.length() - 1);
}
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(finalUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (!headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
connection.setRequestProperty(key, value);
}
}
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "---------->" + map.get(key));
}
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
connection.disconnect();
System.out.println("--------->>> GET request end <<<----------");
}
return result;
}
public static String post(String requestUrl) {
return post(requestUrl, null, null);
}
public static String post(String requestUrl, String param) {
return post(requestUrl, param, new HashMap<String, String>());
}
public static String post(String requestUrl, Map<String, String> param) {
String params = "";
if (!param.isEmpty()) {
Set<Map.Entry<String, String>> entries = param.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
String s = key + "=" + value + "&";
params += s;
}
}
return post(requestUrl, params.substring(0, params.length() - 1));
}
public static String post(String requestUrl, String param, Map<String, String> headers) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(requestUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (!headers.isEmpty()) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
connection.setRequestProperty(key, value);
}
}
os = connection.getOutputStream();
if (param != null && param.length() > 0) {
os.write(param.getBytes());
}
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
sbf.append(line);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
connection.disconnect();
System.out.println("--------->>> POST request end <<<----------");
}
return result;
}
}