50,545
社区成员
发帖
与我相关
我的任务
分享
public boolean login(HttpClient client, String bbsUrl, String username,
String password, String formhash, String charset) {
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
//System.setProperty("apache.commons.httpclient.cookiespec", "BROWSER_COMPATIBILITY ");
String url = bbsUrl + "/logging.php";
PostMethod postMethod = new PostMethod(url, charset);
// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair("loginfield", "username"),
new NameValuePair("username", username),
new NameValuePair("password", password),
new NameValuePair("formhash", formhash),
new NameValuePair("questionid", "0"),
new NameValuePair("answer", ""),
new NameValuePair("cookietime", "315360000"),
new NameValuePair("loginmode", "normal"),
new NameValuePair("action", "login"),
new NameValuePair("loginsubmit", "true") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行
try {
int status = client.executeMethod(postMethod);
//用stream读
String line;
InputStream in = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while((line = br.readLine())!= null){
System.out.println(line);
}
br.close();
in.close();
// 301或者302
if (status == HttpStatus.SC_MOVED_PERMANENTLY
|| status == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
if (locationHeader != null) {
//System.out.println(locationHeader);
}
}
postMethod.releaseConnection();
String geturl1 = "http://jxsj.bbs.xoyo.com/post.php?action=newthread&fid=7079";
String geturl2 = "http://jxsj.bbs.xoyo.com/forumdisplay.php?fid=7079&page=1";
GetMethod get = new GetMethod(geturl2);
//client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
get.setRequestHeader("Host", "jxsj.bbs.xoyo.com");
get.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");
get.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
get.setRequestHeader("Accept-Language", "zh-cn,en;q=0.8,ja;q=0.5,zh-tw;q=0.3");
//get.setRequestHeader("Accept-Encoding", "gzip,deflate");
get.setRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
get.setRequestHeader("Keep-Alive", "115");
get.setRequestHeader("Connection", "keep-alive");
get.setRequestHeader("Referer", "http://jxsj.bbs.xoyo.com/forumdisplay.php?fid=7079&page=2");
client.executeMethod(get);
String line2;
InputStream in2 = get.getResponseBodyAsStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
while((line2 = br2.readLine())!= null){
//System.out.println(line2);
}
br2.close();
in2.close();
get.releaseConnection();
//PrintInfo.printInfo(postMethod);
return true;
} catch (Exception e) {
return false;
}
}