Apache HttpClient模拟登陆网站(待续)
2007-04-05 16:21
375 查看
网上有一些相册的下载工具,设置好帐号密码就可以下图片到本地。现在用java代码模拟一下。
HTTPClient的包有类似的例子。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void test() throws HttpException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String HOST = "photo.server.net";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final int PORT = 80;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String getMethodStr= "/userAccount/1524712";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpClient client = new HttpClient();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.getHostConfiguration().setHost(HOST, PORT, "http");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get first
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GetMethod getMethod = new GetMethod(getMethodStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(getMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("------ Get content /userAccount/1524712: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getRequestCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Header[] headers = getMethod.getResponseHeaders();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Header header : headers) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(header.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get the useful session
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//CookieSpec cookieSpec = CookiePolicy.getDefaultSpec();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Cookie[] getCookies = client.getState().getCookies();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("----- -Useful cookies: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Cookie cookie : getCookies) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(cookie.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//POST password
//其实无论客户端的页面怎么复杂,但是发过去的也只是包,可以使用一些抓包工具获取。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String url = "http://" + HOST + getMethodStr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String encodedUrl = url.replaceAll(":", "%3A");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String postUrl = "/@restrict?furl=" + encodedUrl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PostMethod postMethod = new PostMethod(postUrl);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair text = new NameValuePair("text", "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair pwd = new NameValuePair("pwd", "hello");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair abId = new NameValuePair("ab_id", "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
postMethod.setRequestBody(new NameValuePair[]...{text, pwd, abId});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(postMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("----------------");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("get post feedback:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
postMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Redirect to other page.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int statusCode = postMethod.getStatusCode();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(statusCode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Header locationHeader = postMethod.getResponseHeader("location");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (locationHeader != null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String redirectUri = locationHeader.getValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (redirectUri == null || "".equals(redirectUri)) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
redirectUri = "/";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod = new GetMethod(redirectUri);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(getMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("get redirect:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
headers = getMethod.getResponseHeaders();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Header header : headers) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(header.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
可以看到在一般的网站里面session-id是一直传递的。这里面的很都步骤都是可以使用java的URLConnection的,但有一种情况它不能很好处理. 例如, 登陆成功后, 服务器response.sendRedirect到一个新的URL, 这个时候返回的HTTP相应包应该只有HTTP头的, 里面包含一些返回状态码和要导向的URL地址.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
HTTP/1.0 302 Moved Temporarily
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Date: Sat, 03 Mar 2007 00:00:27 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Server: Microsoft-IIS/6.3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Vary: Accept-Encoding
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Expires: Thu, 19 Nov 1981 08:52:00 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Pragma: no-cache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Location: http://photo.xxxxxx.com/newurl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Type: text/html; charset=UTF-8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Via: 1.0 photo.xxxx.com:8000 (Microsoft-IIS/7.1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: close
碰到(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...这些返回码的时候, URLConnection会主动跳转到这个location, 但是却没传递任何的cookies, 服务器肯定不认帐了. URLConnection虽然有设置requestProperty设置请求的属性, 但上面这种情况好像是无法设置cookies头的, 而且他提供的设置setDefaultProperty好像也不管用. 甚至你连location也无法获取到.
顺便在这里提一下, 一些防盗链接其实可以用cookies头, 如果是经过当前网站来的请求, 藏点东西在cookies头里面; 还有一种可能是用Referer, 请求一个页面的时候, 不敢确定是不是因为返回了Cache-Control这些头,之后再去该网站拿其他东西的时候都要发送个Referer过去.
//第一次请求的返回包
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
HTTP/1.0 200 OK
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Date: Fri, 02 Mar 2007 23:54:04 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Server: Microsoft-IIS/6.3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Vary: Accept-Encoding
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Pragma: no-cache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Set-Cookie: FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491; path=/; domain=.photo.xxxxx.net
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Expires: Thu, 19 Nov 1981 08:52:00 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Type: text/html; charset=UTF-8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Encoding: gzip
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Length: 2762
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Via: 1.0 photo.xxxx.net:8000 (Microsoft-IIS/7.1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: keep-alive
//再次请求包
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Host: s.photo.xxxxx.net
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept: image/png,*/*;q=0.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Language: zh-cn,zh;q=0.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Encoding: gzip,deflate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Keep-Alive: 300
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: keep-alive
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Referer: http://photo.xxx.net/userName/1524712
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cookie: FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491
可以看到HttpClient还是很好用的, 用它甚至可以做一些简单的程序做点自动化的web页面的测试d.
用这个破解别人的相册? 估计效率是不怎么行的, 服务器狠些,再加个随机图片验证,或者几次失败之后封杀你的连接:)
HTTPClient的包有类似的例子。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void test() throws HttpException, IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String HOST = "photo.server.net";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final int PORT = 80;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String getMethodStr= "/userAccount/1524712";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpClient client = new HttpClient();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.getHostConfiguration().setHost(HOST, PORT, "http");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get first
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GetMethod getMethod = new GetMethod(getMethodStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(getMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("------ Get content /userAccount/1524712: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getRequestCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Header[] headers = getMethod.getResponseHeaders();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Header header : headers) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(header.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get the useful session
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//CookieSpec cookieSpec = CookiePolicy.getDefaultSpec();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Cookie[] getCookies = client.getState().getCookies();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("----- -Useful cookies: ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Cookie cookie : getCookies) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(cookie.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//POST password
//其实无论客户端的页面怎么复杂,但是发过去的也只是包,可以使用一些抓包工具获取。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String url = "http://" + HOST + getMethodStr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String encodedUrl = url.replaceAll(":", "%3A");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
final String postUrl = "/@restrict?furl=" + encodedUrl;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PostMethod postMethod = new PostMethod(postUrl);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair text = new NameValuePair("text", "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair pwd = new NameValuePair("pwd", "hello");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
NameValuePair abId = new NameValuePair("ab_id", "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
postMethod.setRequestBody(new NameValuePair[]...{text, pwd, abId});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(postMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("----------------");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("get post feedback:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
postMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Redirect to other page.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int statusCode = postMethod.getStatusCode();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(statusCode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Header locationHeader = postMethod.getResponseHeader("location");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (locationHeader != null) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String redirectUri = locationHeader.getValue();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (redirectUri == null || "".equals(redirectUri)) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
redirectUri = "/";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod = new GetMethod(redirectUri);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
client.executeMethod(getMethod);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println("get redirect:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
headers = getMethod.getResponseHeaders();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (Header header : headers) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.err.println(header.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printOutStream(getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
getMethod.releaseConnection();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
可以看到在一般的网站里面session-id是一直传递的。这里面的很都步骤都是可以使用java的URLConnection的,但有一种情况它不能很好处理. 例如, 登陆成功后, 服务器response.sendRedirect到一个新的URL, 这个时候返回的HTTP相应包应该只有HTTP头的, 里面包含一些返回状态码和要导向的URL地址.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
HTTP/1.0 302 Moved Temporarily
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Date: Sat, 03 Mar 2007 00:00:27 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Server: Microsoft-IIS/6.3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Vary: Accept-Encoding
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Expires: Thu, 19 Nov 1981 08:52:00 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Pragma: no-cache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Location: http://photo.xxxxxx.com/newurl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Type: text/html; charset=UTF-8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Via: 1.0 photo.xxxx.com:8000 (Microsoft-IIS/7.1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: close
碰到(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY)...这些返回码的时候, URLConnection会主动跳转到这个location, 但是却没传递任何的cookies, 服务器肯定不认帐了. URLConnection虽然有设置requestProperty设置请求的属性, 但上面这种情况好像是无法设置cookies头的, 而且他提供的设置setDefaultProperty好像也不管用. 甚至你连location也无法获取到.
顺便在这里提一下, 一些防盗链接其实可以用cookies头, 如果是经过当前网站来的请求, 藏点东西在cookies头里面; 还有一种可能是用Referer, 请求一个页面的时候, 不敢确定是不是因为返回了Cache-Control这些头,之后再去该网站拿其他东西的时候都要发送个Referer过去.
//第一次请求的返回包
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
HTTP/1.0 200 OK
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Date: Fri, 02 Mar 2007 23:54:04 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Server: Microsoft-IIS/6.3
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Vary: Accept-Encoding
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Pragma: no-cache
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Set-Cookie: FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491; path=/; domain=.photo.xxxxx.net
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Expires: Thu, 19 Nov 1981 08:52:00 GMT
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Type: text/html; charset=UTF-8
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Encoding: gzip
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Content-Length: 2762
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Via: 1.0 photo.xxxx.net:8000 (Microsoft-IIS/7.1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: keep-alive
//再次请求包
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Host: s.photo.xxxxx.net
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept: image/png,*/*;q=0.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Language: zh-cn,zh;q=0.5
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Encoding: gzip,deflate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Keep-Alive: 300
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Connection: keep-alive
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Referer: http://photo.xxx.net/userName/1524712
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Cookie: FOTOSSID=a2d1a5c9f34308c3b49ad88813f11491
可以看到HttpClient还是很好用的, 用它甚至可以做一些简单的程序做点自动化的web页面的测试d.
用这个破解别人的相册? 估计效率是不怎么行的, 服务器狠些,再加个随机图片验证,或者几次失败之后封杀你的连接:)
相关文章推荐
- 使用HttpClient模拟网站登陆sina邮箱(保持远程服务器session和cookie)
- 用Apache httpclient模拟登陆学生个人信息系统
- httpclient模拟需要登陆之后才能访问第三方网站(并且需要一些人工参与的操作)
- HttpClient4登陆有验证码的网站
- 详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- 一个比较笨的模拟登陆其他网站的方法
- 【教程】模拟登陆网站 之 C#版(内含两种版本的完整的可运行的代码)
- 详解抓取网站,模拟登陆,抓取动态网页的原理和实现
- 模拟登陆某大论坛网站(基于商业秘密不注明名称)
- 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站
- Apache2.4 Virtual Hosts配置:模拟真实网站+同时开发多个Web项目
- 玩玩网络课堂的外挂-网站模拟登陆
- 使用scrapy 模拟登陆网站后 抓取会员中心相关信息
- httpclient模拟登陆facebook.com
- 网页版的模拟登陆有验证码的网站
- 手把手教你如何利用工具(IE9的F12)去分析模拟登陆网站(百度首页)的内部逻辑过程
- C# 模拟网站登陆
- 模拟登陆网站 之 Python版(内含两种版本的完整的可运行的代码)
- php模拟post提交数据,用处很多,可用来网站的采集,登陆等等
- 使用C#的HttpWebRequest模拟登陆网站