Java爬虫开发中, 需要提交Http协议数据包到目标服务器, 就写了一个简易版的HttpClient类.
类名:HttpClient.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class HttpClient {
//类全局HTTP对象
HttpURLConnection conn = null;
//打开网址
public boolean Open(String sMethod, String sUrl) {
// 创建远程url连接对象
URL url;
try {
url = new URL(sUrl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
conn = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
conn.setRequestMethod(sMethod);
if(sMethod.toUpperCase() == "POST")
{
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
conn.setDoOutput(true);
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
conn.setDoInput(true);
}
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
//设置超时时间
public void SetTimeOut(int iRequest, int iResponse) {
// 设置连接主机服务器的超时时间:15000毫秒
conn.setConnectTimeout(iRequest);
// 设置读取远程返回的数据时间:15000毫秒
conn.setReadTimeout(iResponse);
}
//设置是否允许重定向
public void SetRedirects(boolean bFlg) {
conn.setInstanceFollowRedirects(bFlg);
}
//设置附加协议参数
public void SetRequestHeader(String sHeaderName, String sHeaderValue) {
conn.setRequestProperty(sHeaderName, sHeaderValue);
}
//发送请求
public boolean Send() {
// 发送请求
try {
conn.connect();
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
//发送请求带参数
public boolean Send(String param) {
// 通过连接对象获取一个输出流
OutputStream os;
try {
os = conn.getOutputStream();
// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
os.write(param.getBytes());
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//获取返回状态代码
public int GetResponseStatus() {
try {
return conn.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
//获取指定返回头
public String GetResponseHeader(String sHeaderName) {
return conn.getHeaderFields().get(sHeaderName).toString();
}
//获取所有的返回头
public String GetAllResponseHeaders() {
//读取头部
String strHeader = "";
Map<String, List<String>> headers = conn.getHeaderFields();
for(Map.Entry<String,List<String>> entry : headers.entrySet()){
strHeader+=entry.getKey()+" : ";
for(String value : entry.getValue()){
strHeader+=value+" , ";
}
//去除末尾无效的 逗号
strHeader = strHeader.substring(0, strHeader.length() -2);
strHeader += "\r\n";
}
return strHeader;
}
//获取返回文本
public String GetResponseText(String sEncode) {
if(sEncode == null || sEncode == "")
sEncode = "UTF-8";
InputStream inStream = null;
BufferedReader buffer = null;
String result = null;// 返回结果字符串
try {
inStream = conn.getInputStream();
// 封装输入流is,并指定字符集
buffer = new BufferedReader(new InputStreamReader(inStream, sEncode));
// 存放数据
StringBuffer strBuffer = new StringBuffer();
String temp = null;
while ((temp = buffer.readLine()) != null) {
strBuffer.append(temp);
strBuffer.append("\r\n");
}
result =strBuffer.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
//获取返回流
public InputStream GetResponseBody() {
InputStream inStream = null;
try {
inStream = conn.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inStream;
}
//保存流到文件
public void SaveDataStream(InputStream is, File file) {
try (BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
) {
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//关闭连接
public void Colse() {
conn.disconnect();
}
}
调用方法:
String sPostData = "FengziSoft";
String sLoginUrl = "https://www.test.com/UserLogin.aspx";
http.Open("POST", sLoginUrl);
http.SetTimeOut(15000, 6000);
http.SetRedirects(false);
http.SetRequestHeader("Content-Length", Integer.toString(sPostData.length()) );
http.SetRequestHeader("Upgrade-Insecure-Requests", "1");
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.SetRequestHeader("User-Agent", sUserAgent);
http.SetRequestHeader("Referer", "https://www.test.com/");
if(http.Send(sPostData)) {
if(http.GetResponseStatus() != -1)
{
String sHeader = http.GetAllResponseHeaders();
System.out.println(sHeader);
String sRet = http.GetResponseText("UTF-8");
System.out.println(sRet);
}
}
http.Colse();