package cn.bestmk.utils;
import java.io.*;
import java.net.*;
import java.util.HashMap;
/**
* 简单封装Http请求
*/
public class HttpUtil {
private Proxy proxy = null;
private String cookie = "";
private String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
private HashMap<String, String> headers = new HashMap<>();
public static HttpUtil getInstance() {
return new HttpUtil();
}
/**
* 设置代理
*
* @param host 代理主机地址
* @param port 代理端口
* @return
*/
public HttpUtil setProxy(String host, int port) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
return this;
}
/**
* 设置头部请求信息
*
* @param key
* @param value
* @return
*/
public HttpUtil putHeader(String key, String value) {
headers.put(key, value);
return this;
}
/**
* 设置user-agent
*
* @param ua
* @return
*/
public HttpUtil setUa(String ua) {
this.ua = ua;
return this;
}
/**
* 设置cookie
*
* @param cookie
* @return
*/
public HttpUtil setCookie(String cookie) {
this.cookie = cookie;
return this;
}
/**
* 向指定URL发送GET方法的请求
*
* @param url 发送请求的URL
* @return URL 所代表远程资源的响应结果
*/
public String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
if (proxy != null) {
connection = realUrl.openConnection(proxy);
} else {
connection = realUrl.openConnection();
}
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", ua);
if (!cookie.isEmpty()) {
connection.setRequestProperty("Cookie", cookie);
}
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line + "\n";
}
} catch (Exception e) {
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = null;
// 打开和URL之间的连接
if (proxy != null) {
conn = realUrl.openConnection(proxy);
} else {
conn = realUrl.openConnection();
}
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", ua);
if (!cookie.isEmpty()) {
conn.setRequestProperty("Cookie", cookie);
}
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key));
}
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
使用方法
HttpUtil http=HttpUtil.getInstance().setUa("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36").
setProxy("127.0.0.1",1080),
putHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
String html=http.sendGet("http://www.bestmk.cn/");
System.out.println(html);