入手爬虫有一段时间,不过之前一直使用的是apache的httpclient。但感觉每次引包太过麻烦,而且非常恶心,非得引它的核心包才行。于是就用jdk自带的HttpURLConnection写了一个,用来登录豆瓣网,但是小弟实在不知道是哪里出了问题。今天贴出代码。望大家帮忙找找原因
HttpUtils.java源码:
package Utils;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.json.JSONObject;
import Entity.HttpEntity;
/**
* Http工具类
* @author Administrator
*
*/
public class HttpUtils {
/**
* 发送get请求
* @param url
* @param map 请求头信息
* @return
* @throws Exception
*/
public static HttpEntity Get(URL url,HttpEntity entity)throws Exception{
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
if (entity!=null) {
urlconn.setRequestProperty("Cookie", entity.getMap().get("Set-Cookie").toString());
}
return InputStreamToHttpEntity(urlconn,"utf-8");
}
/**
* 发送post请求
* @param url
* @param params 请求表单内容
* @param map 请求头信息
* @return
* @throws Exception
*/
public static HttpEntity Post(URL url, String params,HttpEntity entity)throws Exception{
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
System.out.println(urlconn.getURL().toString());
if (entity!=null) {
urlconn.setRequestProperty("Cookie", entity.getCookies());
System.out.println(entity.getCookies());
}
urlconn.setInstanceFollowRedirects(false);
urlconn.setRequestMethod("POST");
urlconn.setDoOutput(true);
urlconn.setDoInput(true);
PrintWriter out = new PrintWriter(urlconn.getOutputStream());
out.print(params);
out.flush();
return InputStreamToHttpEntity(urlconn, "utf-8");
}
/**
* 通过HttpURLConnection生成成HttpEntity对象
* @param is
* @return
*/
public static HttpEntity InputStreamToHttpEntity(HttpURLConnection urlconn){
HttpEntity entity = IsJump(urlconn);
try {
HttpURLConnection conn = (HttpURLConnection)entity.getLocationurl().openConnection();
if (entity.getCookies()!=null && !"".equals(entity.getCookies())) {
conn.setRequestProperty("Cookie", entity.getCookies());
}
byte[] data = new byte[8192];
StringBuffer rs = new StringBuffer();
while (conn.getInputStream().read(data)!=-1) {
rs.append(new String(data));
}
entity.setContent(rs.toString());
entity.setStatCode(conn.getResponseCode());
entity.setMap(conn.getHeaderFields());
if (conn.getHeaderField("Location")==null) {
entity.setLocationurl(null);
}else {
entity.setLocationurl(new URL(conn.getHeaderField("Location")));
}
} catch (IOException e1) {
e1.printStackTrace();
}
return entity;
}
private static HttpEntity IsJump(HttpURLConnection urlconn){
HttpEntity entity = new HttpEntity();
try {
if (urlconn.getResponseCode()==302) {
String cookies = "";
Map<String, List<String>> map = urlconn.getHeaderFields();
List<String> list = map.get("Set-Cookie");
for (int i=0;i<list.size();i++) {
cookies+=(list.get(i));
}
cookies = cookies.substring(0, cookies.indexOf(";"));
entity.setCookies(cookies);
System.out.println(cookies);
entity.setLocationurl(new URL(map.get("Location").get(0)));
}else {
entity.setLocationurl(urlconn.getURL());
}
} catch (IOException e) {
e.printStackTrace();
}
return entity;
}
/**
* 通过HttpURLConnection生成成HttpEntity对象
* @param is
* @param Encode 输出编码
* @return
*/
public static HttpEntity InputStreamToHttpEntity(HttpURLConnection urlconn, String Encode){
HttpEntity entity = InputStreamToHttpEntity(urlconn);
try {
entity.setContent(new String(entity.getContent().getBytes(),Encode));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return entity;
}
/**
* 下载手动输入验证码
* @param url
* @param entity
* @return
*/
public static String DownAndInputCode(URL url,HttpEntity entity){
String result = "";
OutputStream out = null;
try {
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
if (entity!=null) {
urlconn.setRequestProperty("Cookie", entity.getMap().get("Set-Cookie").toString());
}
File file = new File("D:/test/yzm/yzm.jpg");
out = new FileOutputStream(file);
byte[] data = new byte[8192];
while (urlconn.getInputStream().read(data)!=-1) {
out.write(data);
}
JFrame frame = new JFrame();
frame.setVisible(false);
frame.setBounds(300, 100, 300, 100);
frame.setLayout(new FlowLayout());
ImageIcon icon = new ImageIcon("D:/test/yzm/yzm.jpg");
frame.add(new JLabel(icon));
frame.setVisible(true);
System.out.print("输入你看到的验证码:");
Scanner scr = new Scanner(System.in);
result = scr.nextLine();
System.out.println("验证码 : " + result);
frame.dispose();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static void main(String[] args)throws Exception {
HttpEntity entity = Get(new URL("http://www.douban.com/j/misc/captcha"),null);
Map map = entity.getMap();
JSONObject jsonObject = new JSONObject(entity.getContent());
String captcha_id = jsonObject.get("url").toString();
captcha_id = captcha_id.substring(captcha_id.indexOf("id="), captcha_id.indexOf("&size")).replace("id=", "");
String code = DownAndInputCode(new URL("http:"+jsonObject.get("url").toString()),entity);
String params = "form_email=363266400@qq.com&form_password=qweqweqwe123&source=index_nav" +
"&captcha-solution="+code+"&captcha-id="+captcha_id;
HttpEntity loginentity = Post(new URL("https://www.douban.com/accounts/login"), params, entity);
System.out.println(loginentity.getContent());
}
}
还有一个封装的返回对象类HttpEntity.java :
package Entity;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class HttpEntity {
private Map<String, List<String>> map; //头信息
private String content; //返回的html内容
private int StatCode; //返回码
private String Cookies; //cookies
private URL Locationurl; //Location地址
public URL getLocationurl() {
return Locationurl;
}
public void setLocationurl(URL locationurl) {
Locationurl = locationurl;
}
public String getCookies() {
return Cookies;
}
public void setCookies(String cookies) {
Cookies = cookies;
}
public int getStatCode() {
return StatCode;
}
public void setStatCode(int statCode) {
StatCode = statCode;
}
public Map<String, List<String>> getMap() {
return map;
}
public void setMap(Map<String, List<String>> map) {
this.map = map;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
最后本来还引入了一个jar包用来解析json数据的。没有用apache,因为用它的话又要引它核心的包。坑!!!!,所以网上找了一下,使用的是json.org.jar。小弟还不会上传附件。只好各位大神们百度一下啦