httpclient对discuz论坛的问题

xuganggogo 2010-06-12 05:23:38
这两天研究了下,关于discuz论坛的发帖,用httpclient实现无验证码的自动发帖。
但是发现有的discuz论坛可以正常发送成功,有的却发送失败。

检查后发现失败的原因是,登录成功后,session没有保存下来,导致访问任何页面都是未登陆状态。不知为什么?求解答

贴出部分方法

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;
}
}


...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuganggogo 2010-06-13
  • 打赏
  • 举报
回复
我先顶一个,希望有朋友能给出其他的见解。
xuganggogo 2010-06-13
  • 打赏
  • 举报
回复
嗯。结贴了,用httpcore4.0就好了,看样子要抛弃httpclient3.1了。
izard999 2010-06-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ivorytower 的回复:]
参考这里httpclient写的DISCUZ论坛盖楼器
[/Quote]
就去看这个吧
xuganggogo 2010-06-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ivorytower 的回复:]

参考这里httpclient写的DISCUZ论坛盖楼器
[/Quote]

3q ! 我回去看看
xuganggogo 2010-06-12
  • 打赏
  • 举报
回复
我之前也做了很多个大型博客的回帖发帖。也碰到过很多问题,都解决了。
这个discuz论坛,发帖很简单。
不过碰到今天的问题,我也是很奇怪……
希望有朋友能解答!
  • 打赏
  • 举报
回复
Java技术栈 2010-06-12
  • 打赏
  • 举报
回复
没人回答 莫非题很难 部分代码看了下 没有看出端倪 端倪

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧