使用HttpClient模拟网站登陆sina邮箱(保持远程服务器session和cookie) (转载)
http://www.cnblogs.com/yesun/archive/2007/09/23/903319.html
/**
* 通过URL取得网页内容
*/
private void HttpRequestURL()
{
String postData = "";
if(_tc.getPostData()!=null && !_tc.getPostData().equals(""))
{
postData = _tc.getPostData();
}
if(_tc.getPostMethod()==null || _tc.getPostMethod().equals(""))
_tc.setPostMethod("GET");
String postUrl = _tc.getUrl();
if(_tc.getPostMethod().toUpperCase().equals("GET") && !postData.equals(""))
{
if(postUrl.indexOf("?") > -1)
postUrl += "&"+postData;
else
postUrl += "?"+postData;
}
System.out.println("--------Transfer Start------------------------------------------------------------------");
System.out.println(" 页面解析准备就绪...");
System.out.println(" Url:"+postUrl);
System.out.println(" PostData:"+postData);
System.out.println(" PostMethod:"+_tc.getPostMethod());
System.out.println(" Encoding:"+_tc.getEncoding());
System.out.println("--------------------------------------------------------------------------------------");
HttpClient httpClient = new HttpClient();
if(_tc.getPostMethod().toUpperCase().equals("GET"))
{
if(!postData.equals(""))
{
if(postUrl.indexOf("?") > -1)
postUrl += "&"+postData;
else
postUrl += "?"+postData;
}
GetMethod getHC = new GetMethod(postUrl);
getHC.setFollowRedirects(false);
getHC.getParams().setHttpElementCharset("UTF-8");
//发送Cookie
StringBuilder cookieValue = new StringBuilder();
for(String key : this.cookie.keySet()){
cookieValue.append(key);
cookieValue.append("=");
cookieValue.append(this.cookie.get(key));
cookieValue.append("; ");
}
getHC.setRequestHeader("Cookie", cookieValue.toString());
getHC.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
int statusCode;
try {
statusCode = httpClient.executeMethod(getHC);
Header[] headers = getHC.getResponseHeaders();
for(Header header : headers){
if(header.getElements().length > 1){
HeaderElement[] elements = header.getElements();
for(HeaderElement element : elements){
checkCookie(element);
}
}
else
{
checkCookie(header);
}
}
if(statusCode==HttpStatus.SC_OK){
String charset = "utf-8";
if(_tc.getEncoding() == GB2312)
charset = "gb2312";
else if(_tc.getEncoding() == UTF8)
charset = "utf-8";
else if(_tc.getEncoding() == BIG5)
charset = "big5";
else
charset = "utf-8";
System.out.println("charset : " + charset);
InputStreamReader isr = new InputStreamReader(getHC.getResponseBodyAsStream(), charset); // 设置读取流的编码格式,自定义编码
// 使用字符读取方式,循环读取源文件内容
StringBuffer sb = new StringBuffer();
int b ;
while ((b = isr.read()) != -1)//顺序读取文件text里的内容并赋值给整型变量b,直到文件结束为止。
{
if (b < 32 && b!= 10 && b != 13 && b != 9) b = 32;//过滤掉一些换行等符号
//if ( b== 10 || b== 13 || b== 9) b = 32;//过滤掉一些换行等符号
sb.append((char)b);
}
isr.close();
getHC.abort();
//取得采集的内容
_content = sb.toString();
//System.out.println("content : "+_content);
}
else
{
if(statusCode == 301 || statusCode == 302){
Header locationHeader = getHC.getResponseHeader("location");
_tc.setUrl(locationHeader.getValue());
_tc.setPostData("");
_tc.setPostMethod("GET");
HttpRequestURL();//重新请求新网页
}
}
} catch (HttpException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
else
{
PostMethod postHC = new PostMethod(postUrl);
postHC.setFollowRedirects(false);
postHC.getParams().setHttpElementCharset("UTF-8");
//发送Cookie
StringBuilder cookieValue = new StringBuilder();
for(String key : this.cookie.keySet()){
cookieValue.append(key);
cookieValue.append("=");
cookieValue.append(this.cookie.get(key));
cookieValue.append("; ");
}
postHC.setRequestHeader("Cookie", cookieValue.toString());
postHC.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
//post数据到服务器
if(postData!=null && !postData.equals("")){
HashMap<String,String> argv = new HashMap<String,String>();
String[] arrParams = postData.split("&");
for(int i=0;i<arrParams.length;i++){
if(arrParams[i] == null || arrParams[i].equals(""))
{
continue;
}
else if(arrParams[i].indexOf("=") > -1)
{
String[] nameValue = arrParams[i].split("=");
if(nameValue.length > 1)
argv.put(nameValue[0], nameValue[1]);
}
}
if(argv!=null){
NameValuePair[] nameValuePair = new NameValuePair[argv.size()];
int i = 0;
for(String key : argv.keySet()){
nameValuePair[i] = new NameValuePair(key, argv.get(key));
i++;
}
postHC.setRequestBody(nameValuePair);
}
}
int statusCode;
try {
statusCode = httpClient.executeMethod(postHC);
Header[] headers = postHC.getResponseHeaders();
for(Header header : headers){
if(header.getElements().length > 1){
HeaderElement[] elements = header.getElements();
for(HeaderElement element : elements){
checkCookie(element);
}
}
else
{
checkCookie(header);
}
}
if(statusCode==HttpStatus.SC_OK){
String charset = "utf-8";
if(_tc.getEncoding() == GB2312)
charset = "gb2312";
else if(_tc.getEncoding() == UTF8)
charset = "utf-8";
else if(_tc.getEncoding() == BIG5)
charset = "big5";
else
charset = "utf-8";
InputStreamReader isr = new InputStreamReader(postHC.getResponseBodyAsStream(), charset); // 设置读取流的编码格式,自定义编码
// 使用字符读取方式,循环读取源文件内容
StringBuffer sb = new StringBuffer();
int b ;
while ((b = isr.read()) != -1)//顺序读取文件text里的内容并赋值给整型变量b,直到文件结束为止。
{
if (b < 32 && b!= 10 && b != 13 && b != 9) b = 32;//过滤掉一些换行等符号
//if ( b== 10 || b== 13 || b== 9) b = 32;//过滤掉一些换行等符号
sb.append((char)b);
}
isr.close();
postHC.abort();
//取得采集的内容
_content = sb.toString();
}
else
{
if(statusCode == 301 || statusCode == 302){
Header locationHeader = postHC.getResponseHeader("location");
_tc.setUrl(locationHeader.getValue());
_tc.setPostData("");
_tc.setPostMethod("GET");
HttpRequestURL();//重新请求新网页
}
}
} catch (HttpException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
private void checkCookie(NameValuePair entry){
if(entry.getName().equals("Set-Cookie")){
//这个就是Cookie了
String value = entry.getValue();
if(value.indexOf(";") >0 )
value = value.substring(0,value.indexOf(";"));
System.out.println(entry.toString());
String[] cookieNameValuePair = value.split("=");
if(cookieNameValuePair.length > 1){
if(cookieNameValuePair[1].equals("deleted")){
this.cookie.remove(cookieNameValuePair[0]);
}
else
{
this.cookie.put(cookieNameValuePair[0], cookieNameValuePair[1]);
}
}
else
{
this.cookie.remove(cookieNameValuePair[0]);
}
}
}
/**
* 通过URL取得网页内容
*/
private void HttpRequestURL()
{
String postData = "";
if(_tc.getPostData()!=null && !_tc.getPostData().equals(""))
{
postData = _tc.getPostData();
}
if(_tc.getPostMethod()==null || _tc.getPostMethod().equals(""))
_tc.setPostMethod("GET");
String postUrl = _tc.getUrl();
if(_tc.getPostMethod().toUpperCase().equals("GET") && !postData.equals(""))
{
if(postUrl.indexOf("?") > -1)
postUrl += "&"+postData;
else
postUrl += "?"+postData;
}
System.out.println("--------Transfer Start------------------------------------------------------------------");
System.out.println(" 页面解析准备就绪...");
System.out.println(" Url:"+postUrl);
System.out.println(" PostData:"+postData);
System.out.println(" PostMethod:"+_tc.getPostMethod());
System.out.println(" Encoding:"+_tc.getEncoding());
System.out.println("--------------------------------------------------------------------------------------");
HttpClient httpClient = new HttpClient();
if(_tc.getPostMethod().toUpperCase().equals("GET"))
{
if(!postData.equals(""))
{
if(postUrl.indexOf("?") > -1)
postUrl += "&"+postData;
else
postUrl += "?"+postData;
}
GetMethod getHC = new GetMethod(postUrl);
getHC.setFollowRedirects(false);
getHC.getParams().setHttpElementCharset("UTF-8");
//发送Cookie
StringBuilder cookieValue = new StringBuilder();
for(String key : this.cookie.keySet()){
cookieValue.append(key);
cookieValue.append("=");
cookieValue.append(this.cookie.get(key));
cookieValue.append("; ");
}
getHC.setRequestHeader("Cookie", cookieValue.toString());
getHC.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
int statusCode;
try {
statusCode = httpClient.executeMethod(getHC);
Header[] headers = getHC.getResponseHeaders();
for(Header header : headers){
if(header.getElements().length > 1){
HeaderElement[] elements = header.getElements();
for(HeaderElement element : elements){
checkCookie(element);
}
}
else
{
checkCookie(header);
}
}
if(statusCode==HttpStatus.SC_OK){
String charset = "utf-8";
if(_tc.getEncoding() == GB2312)
charset = "gb2312";
else if(_tc.getEncoding() == UTF8)
charset = "utf-8";
else if(_tc.getEncoding() == BIG5)
charset = "big5";
else
charset = "utf-8";
System.out.println("charset : " + charset);
InputStreamReader isr = new InputStreamReader(getHC.getResponseBodyAsStream(), charset); // 设置读取流的编码格式,自定义编码
// 使用字符读取方式,循环读取源文件内容
StringBuffer sb = new StringBuffer();
int b ;
while ((b = isr.read()) != -1)//顺序读取文件text里的内容并赋值给整型变量b,直到文件结束为止。
{
if (b < 32 && b!= 10 && b != 13 && b != 9) b = 32;//过滤掉一些换行等符号
//if ( b== 10 || b== 13 || b== 9) b = 32;//过滤掉一些换行等符号
sb.append((char)b);
}
isr.close();
getHC.abort();
//取得采集的内容
_content = sb.toString();
//System.out.println("content : "+_content);
}
else
{
if(statusCode == 301 || statusCode == 302){
Header locationHeader = getHC.getResponseHeader("location");
_tc.setUrl(locationHeader.getValue());
_tc.setPostData("");
_tc.setPostMethod("GET");
HttpRequestURL();//重新请求新网页
}
}
} catch (HttpException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
else
{
PostMethod postHC = new PostMethod(postUrl);
postHC.setFollowRedirects(false);
postHC.getParams().setHttpElementCharset("UTF-8");
//发送Cookie
StringBuilder cookieValue = new StringBuilder();
for(String key : this.cookie.keySet()){
cookieValue.append(key);
cookieValue.append("=");
cookieValue.append(this.cookie.get(key));
cookieValue.append("; ");
}
postHC.setRequestHeader("Cookie", cookieValue.toString());
postHC.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
//post数据到服务器
if(postData!=null && !postData.equals("")){
HashMap<String,String> argv = new HashMap<String,String>();
String[] arrParams = postData.split("&");
for(int i=0;i<arrParams.length;i++){
if(arrParams[i] == null || arrParams[i].equals(""))
{
continue;
}
else if(arrParams[i].indexOf("=") > -1)
{
String[] nameValue = arrParams[i].split("=");
if(nameValue.length > 1)
argv.put(nameValue[0], nameValue[1]);
}
}
if(argv!=null){
NameValuePair[] nameValuePair = new NameValuePair[argv.size()];
int i = 0;
for(String key : argv.keySet()){
nameValuePair[i] = new NameValuePair(key, argv.get(key));
i++;
}
postHC.setRequestBody(nameValuePair);
}
}
int statusCode;
try {
statusCode = httpClient.executeMethod(postHC);
Header[] headers = postHC.getResponseHeaders();
for(Header header : headers){
if(header.getElements().length > 1){
HeaderElement[] elements = header.getElements();
for(HeaderElement element : elements){
checkCookie(element);
}
}
else
{
checkCookie(header);
}
}
if(statusCode==HttpStatus.SC_OK){
String charset = "utf-8";
if(_tc.getEncoding() == GB2312)
charset = "gb2312";
else if(_tc.getEncoding() == UTF8)
charset = "utf-8";
else if(_tc.getEncoding() == BIG5)
charset = "big5";
else
charset = "utf-8";
InputStreamReader isr = new InputStreamReader(postHC.getResponseBodyAsStream(), charset); // 设置读取流的编码格式,自定义编码
// 使用字符读取方式,循环读取源文件内容
StringBuffer sb = new StringBuffer();
int b ;
while ((b = isr.read()) != -1)//顺序读取文件text里的内容并赋值给整型变量b,直到文件结束为止。
{
if (b < 32 && b!= 10 && b != 13 && b != 9) b = 32;//过滤掉一些换行等符号
//if ( b== 10 || b== 13 || b== 9) b = 32;//过滤掉一些换行等符号
sb.append((char)b);
}
isr.close();
postHC.abort();
//取得采集的内容
_content = sb.toString();
}
else
{
if(statusCode == 301 || statusCode == 302){
Header locationHeader = postHC.getResponseHeader("location");
_tc.setUrl(locationHeader.getValue());
_tc.setPostData("");
_tc.setPostMethod("GET");
HttpRequestURL();//重新请求新网页
}
}
} catch (HttpException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
private void checkCookie(NameValuePair entry){
if(entry.getName().equals("Set-Cookie")){
//这个就是Cookie了
String value = entry.getValue();
if(value.indexOf(";") >0 )
value = value.substring(0,value.indexOf(";"));
System.out.println(entry.toString());
String[] cookieNameValuePair = value.split("=");
if(cookieNameValuePair.length > 1){
if(cookieNameValuePair[1].equals("deleted")){
this.cookie.remove(cookieNameValuePair[0]);
}
else
{
this.cookie.put(cookieNameValuePair[0], cookieNameValuePair[1]);
}
}
else
{
this.cookie.remove(cookieNameValuePair[0]);
}
}
}