首先需要从登录界面中取得验证码的图片保存到本地,并且取得相关的value字段
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class W
{
public static void main(String args[])
{
try
{
Document doc = Jsoup.connect("http://www.douban.com/login").timeout(5000).get();
String infotable = doc.toString();
//System.out.println(infotable);
Elements infoTable = doc.select("img[id]");
Elements info = doc.getElementsByAttributeValue("class", "captcha_block");
//System.out.println(infoTable.toString());
//System.out.println(info.toString());
Elements in = info.select("input");
//System.out.println(in);
Pattern p = Pattern.compile("http://(\\S[^\"]*)");
Matcher m = p.matcher(infoTable.toString());
if(m.find())
System.out.println(m.group());
System.out.println();
Pattern p1 = Pattern.compile("type=\"hidden\".*");
Matcher m1 = p1.matcher(in.toString());
if(m1.find())
System.out.println(m1.group());
Pattern p2 = Pattern.compile("name=\"(\\S[^\"]*)"); //正则匹配
Matcher m2 = p2.matcher(m1.group().toString());
if(m2.find())
System.out.println(m2.group(1));
Pattern p3 = Pattern.compile("value=\"(\\S[^\"]*)");
Matcher m3 = p3.matcher(m1.group().toString());
if(m3.find())
System.out.println(m3.group(1));
URL url = new URL(m.group());
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("D:\\Users\\ibm\\Desktop\\douban.jpg"); //取图片并且保存到本地
FileOutputStream out = new FileOutputStream(file);
int i = 0;
while((i = is.read()) != -1)
{
out.write(i);
}
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
然后就是调用HttpClient中的POST方式向服务器端发送需要的字段给表单,但是出现了302的提示,也就是需要重定向,这是需要取出Header中的Location字段,也就是返回的重定向字段,利用这个字段,重新用get方式向服务器发请求,这时就可以实现登录豆瓣了,注意:第二次重定向的时候,使用get方式发的。
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class WG
{
public static void main(String args[])
{
try
{
/*Document doc = Jsoup.connect("http://www.douban.com/login").timeout(5000).get();
//String infotable = doc.toString();
//System.out.println(infotable);
Elements infoTable = doc.select("img[id]");
Elements info = doc.getElementsByAttributeValue("class", "captcha_block");
//System.out.println(infoTable.toString());
//System.out.println(info.toString());
Elements in = info.select("input");
//System.out.println(in);
Pattern p = Pattern.compile("http://(\\S[^\"]*)");
Matcher m = p.matcher(infoTable.toString());
if(m.find())
System.out.println(m.group());
System.out.println();
Pattern p1 = Pattern.compile("type=\"hidden\".*");
Matcher m1 = p1.matcher(in.toString());
if(m1.find())
System.out.println(m1.group());
Pattern p2 = Pattern.compile("name=\"(\\S[^\"]*)");
Matcher m2 = p2.matcher(m1.group().toString());
if(m2.find())
System.out.println(m2.group(1));
Pattern p3 = Pattern.compile("value=\"(\\S[^\"]*)");
Matcher m3 = p3.matcher(m1.group().toString());
if(m3.find())
System.out.println(m3.group(1));
URL url = new URL(m.group());
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("D:\\Users\\ibm\\Desktop\\douban.jpg");
FileOutputStream out = new FileOutputStream(file);
int i = 0;
while((i = is.read()) != -1)
{
out.write(i);
}
is.close();*/
//System.out.println(doc.tagName("a").toString());
//Elements info = doc.getElementsByAttributeValue("id", "article_h1");
//System.out.println(info.text().toString());
//Elements in = doc.getElementsByAttributeValue("class", "time");
//System.out.println(in.text().toString());
//Elements tableLineInfos = infoTable.select("p");
HttpClient httpClient = new HttpClient();
//GetMethod getMethod = new GetMethod("http://gw.bupt.edu.cn");
//int statusCode = httpClient.executeMethod(getMethod);
//System.out.println(getMethod.getResponseBodyAsString());
PostMethod postMethod = new PostMethod("http://www.douban.com/login");
NameValuePair postData[] = new NameValuePair[5]; //用post方式向服务器提交表单
postData[0] = new NameValuePair("form_email","sunwangdong121212@163.com");
postData[1] = new NameValuePair("form_password","swd85153866");
postData[2] = new NameValuePair("source","index_nav");
postData[3] = new NameValuePair("captcha-solution","brown");
postData[4] = new NameValuePair("captcha-id","ViaOBbzJByYOO4ENTy9XM2F1:en");
postMethod.setRequestBody(postData); //发送表单信息给服务器
//httpClient.executeMethod(postMethod);
//System.out.println("response=" + postMethod.getResponseBodyAsString());
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(statusCode); //返回HTTP状态码
System.out.println("response=" + postMethod.getResponseBodyAsString());
if(statusCode == 302) //因为HttpClient3.1版本默认的Post方式是不能自动进行重定向的,所以需要自行进行重定向操作。
{
Header header = postMethod.getResponseHeader("Location"); //取出返回回来的Header中的location字段,也就是重定向的新的URL
System.out.println(postMethod.getResponseHeader("Location"));
if(header != null)
{
String locationUrl = header.getValue();
//System.out.println(locationUrl);
GetMethod redirect = new GetMethod(locationUrl);
httpClient.executeMethod(redirect); //重新用get方式向服务器端发送重定向信息,得到登录界面的html
System.out.println("response=" + redirect.getResponseBodyAsString());
}
/*Pattern p1 = Pattern.compile("http://.*");
Matcher m1 = p1.matcher(postMethod.getResponseBodyAsString().toString());
if(m1.find())
{
URL url = new URL(m1.group());
URLConnection uc = url.openConnection();
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
InputStream l_urlStream;
l_urlStream = uc.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream,"UTF-8"));
while((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString += sCurrentLine + "\r\n";
}
System.out.println(sTotalString);
}*/
}
postMethod.releaseConnection();
}
catch(HttpException e)
{
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class W
{
public static void main(String args[])
{
try
{
Document doc = Jsoup.connect("http://www.douban.com/login").timeout(5000).get();
String infotable = doc.toString();
//System.out.println(infotable);
Elements infoTable = doc.select("img[id]");
Elements info = doc.getElementsByAttributeValue("class", "captcha_block");
//System.out.println(infoTable.toString());
//System.out.println(info.toString());
Elements in = info.select("input");
//System.out.println(in);
Pattern p = Pattern.compile("http://(\\S[^\"]*)");
Matcher m = p.matcher(infoTable.toString());
if(m.find())
System.out.println(m.group());
System.out.println();
Pattern p1 = Pattern.compile("type=\"hidden\".*");
Matcher m1 = p1.matcher(in.toString());
if(m1.find())
System.out.println(m1.group());
Pattern p2 = Pattern.compile("name=\"(\\S[^\"]*)"); //正则匹配
Matcher m2 = p2.matcher(m1.group().toString());
if(m2.find())
System.out.println(m2.group(1));
Pattern p3 = Pattern.compile("value=\"(\\S[^\"]*)");
Matcher m3 = p3.matcher(m1.group().toString());
if(m3.find())
System.out.println(m3.group(1));
URL url = new URL(m.group());
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("D:\\Users\\ibm\\Desktop\\douban.jpg"); //取图片并且保存到本地
FileOutputStream out = new FileOutputStream(file);
int i = 0;
while((i = is.read()) != -1)
{
out.write(i);
}
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
然后就是调用HttpClient中的POST方式向服务器端发送需要的字段给表单,但是出现了302的提示,也就是需要重定向,这是需要取出Header中的Location字段,也就是返回的重定向字段,利用这个字段,重新用get方式向服务器发请求,这时就可以实现登录豆瓣了,注意:第二次重定向的时候,使用get方式发的。
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class WG
{
public static void main(String args[])
{
try
{
/*Document doc = Jsoup.connect("http://www.douban.com/login").timeout(5000).get();
//String infotable = doc.toString();
//System.out.println(infotable);
Elements infoTable = doc.select("img[id]");
Elements info = doc.getElementsByAttributeValue("class", "captcha_block");
//System.out.println(infoTable.toString());
//System.out.println(info.toString());
Elements in = info.select("input");
//System.out.println(in);
Pattern p = Pattern.compile("http://(\\S[^\"]*)");
Matcher m = p.matcher(infoTable.toString());
if(m.find())
System.out.println(m.group());
System.out.println();
Pattern p1 = Pattern.compile("type=\"hidden\".*");
Matcher m1 = p1.matcher(in.toString());
if(m1.find())
System.out.println(m1.group());
Pattern p2 = Pattern.compile("name=\"(\\S[^\"]*)");
Matcher m2 = p2.matcher(m1.group().toString());
if(m2.find())
System.out.println(m2.group(1));
Pattern p3 = Pattern.compile("value=\"(\\S[^\"]*)");
Matcher m3 = p3.matcher(m1.group().toString());
if(m3.find())
System.out.println(m3.group(1));
URL url = new URL(m.group());
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("D:\\Users\\ibm\\Desktop\\douban.jpg");
FileOutputStream out = new FileOutputStream(file);
int i = 0;
while((i = is.read()) != -1)
{
out.write(i);
}
is.close();*/
//System.out.println(doc.tagName("a").toString());
//Elements info = doc.getElementsByAttributeValue("id", "article_h1");
//System.out.println(info.text().toString());
//Elements in = doc.getElementsByAttributeValue("class", "time");
//System.out.println(in.text().toString());
//Elements tableLineInfos = infoTable.select("p");
HttpClient httpClient = new HttpClient();
//GetMethod getMethod = new GetMethod("http://gw.bupt.edu.cn");
//int statusCode = httpClient.executeMethod(getMethod);
//System.out.println(getMethod.getResponseBodyAsString());
PostMethod postMethod = new PostMethod("http://www.douban.com/login");
NameValuePair postData[] = new NameValuePair[5]; //用post方式向服务器提交表单
postData[0] = new NameValuePair("form_email","sunwangdong121212@163.com");
postData[1] = new NameValuePair("form_password","swd85153866");
postData[2] = new NameValuePair("source","index_nav");
postData[3] = new NameValuePair("captcha-solution","brown");
postData[4] = new NameValuePair("captcha-id","ViaOBbzJByYOO4ENTy9XM2F1:en");
postMethod.setRequestBody(postData); //发送表单信息给服务器
//httpClient.executeMethod(postMethod);
//System.out.println("response=" + postMethod.getResponseBodyAsString());
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(statusCode); //返回HTTP状态码
System.out.println("response=" + postMethod.getResponseBodyAsString());
if(statusCode == 302) //因为HttpClient3.1版本默认的Post方式是不能自动进行重定向的,所以需要自行进行重定向操作。
{
Header header = postMethod.getResponseHeader("Location"); //取出返回回来的Header中的location字段,也就是重定向的新的URL
System.out.println(postMethod.getResponseHeader("Location"));
if(header != null)
{
String locationUrl = header.getValue();
//System.out.println(locationUrl);
GetMethod redirect = new GetMethod(locationUrl);
httpClient.executeMethod(redirect); //重新用get方式向服务器端发送重定向信息,得到登录界面的html
System.out.println("response=" + redirect.getResponseBodyAsString());
}
/*Pattern p1 = Pattern.compile("http://.*");
Matcher m1 = p1.matcher(postMethod.getResponseBodyAsString().toString());
if(m1.find())
{
URL url = new URL(m1.group());
URLConnection uc = url.openConnection();
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
InputStream l_urlStream;
l_urlStream = uc.getInputStream();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream,"UTF-8"));
while((sCurrentLine = l_reader.readLine()) != null)
{
sTotalString += sCurrentLine + "\r\n";
}
System.out.println(sTotalString);
}*/
}
postMethod.releaseConnection();
}
catch(HttpException e)
{
}
catch (IOException e)
{
e.printStackTrace();
}
}
}