登陆论坛成功,转向发帖界面成功,为了测试,我自己搞了一个论坛做为测试,程序的执行过程是这样的,加入我以前注册过会员,我没有登陆,首先去看帖子然后想发帖就点击发帖,这是系统提示我登陆(程序开始登陆---并且成功)我输入相关信息并成功登陆(程序也输入相关信息登陆并转向成功)--打开了发帖页面(程序也获得发帖界面)我开始输入相关信息然后提交表单发帖成功(程序填写表单-----!!!!!发帖失败提示:您的请求来路不正确或验证字串不符,无法提交。如果您安装了某种默认屏蔽来路信息的个人防火墙软件(如 Norton Internet Security),请设置其不要禁止来路信息后再试。</p>)1 我估计是发帖时,有相关的表单没填写但是dz的论坛发帖界面的小表单太多了
2 还有一个理解就是  提交表单的地址,我用的是form上的那个action="post.php?action=newthread&amp;fid=2&amp;extra=page%3D1&amp;topicsubmit=yes"
里面的分号是不是有问题,(是不是要对地址进行处理,发送的数据估计也要处理吧)
3 是不是dz的论坛照我这个方法是完全不可以的了???(请指教)
试验用的论坛地址http://www.kanyixia.net/cs/index.php我吧不用要的功能都给关了,
提交表单用的码:用了HTTPClient包 地址http://www.kanyixia.net/HTTPClient.zip
希望大家提提意见,我不知道错哪里了,
关键是是个新手,好多简单的问题搞不定,希望高人指点指点import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.NVPair;
import HTTPClient.ParseException;public class Beta {
private HTTPConnection connection;
HTTPResponse rsp;
public Beta() {
getConnection("www.kanyixia.net", 80);
} public HTTPClient.HTTPConnection getConnection(String hostName, int port) {
if (connection == null) {
try {
connection = new HTTPClient.HTTPConnection(hostName, port);
HTTPClient.CookieModule.setCookiePolicyHandler(null);
connection.addDefaultModule(Class.forName("HTTPClient.CookieModule"), 1);
connection.addModule(Class.forName("HTTPClient.RedirectionModule"), 2);
} catch (Exception e) {
e.printStackTrace();
}
}
return connection;
} public void login() throws ParseException { WebRequester wr = WebRequester.getInstance();
//NVPair[] formdata = new NVPair[10];
// 对应登陆需要的表单字段填写
NVPair[] formdata =new NVPair[9];
formdata[0]=new NVPair("formhash","f8e40c63");
formdata[1]=new NVPair("referer","");
formdata[2]=new NVPair("cookietime","2592000");
formdata[3]=new NVPair("loginfield","username");
formdata[4]=new NVPair("username","chowqingbao");  //登陆的用户名
formdata[5]=new NVPair("password","19871124");   //登陆密码
formdata[6]=new NVPair("questionid","0");
formdata[7]=new NVPair("answer","2592000");
formdata[8]=new NVPair("loginsubmit","会员登录");
// 提交到指定登陆页面
         wr.request(connection, "cs/post.php?action=newthread&fid=2&extra=page%3D1", "post", null);
 System.out.println("-------------------------------------------------------转向登陆页面并登陆进入--------------------------------------------------------------------------");
 wr.request(connection, "cs/logging.php?action=login", "post", formdata);  
 System.out.println("---------------------------------------------------------登陆成功------------------------------------------------------------------------");
 wr.request(connection, "cs/forumdisplay.php?fid=2", "post", null);
 System.out.println("---------------------------------------------------------转向发帖页面成功------------------------------------------------------------------------");
NVPair[] form_data = new NVPair[16];
form_data[0]=new NVPair("formhash","a4921434");
form_data[1]=new NVPair("isblog","");
form_data[2]=new NVPair("frombbs","1");
form_data[3]=new NVPair("typeid","10");
form_data[4] = new NVPair("subject","有人说理工的论坛不可以发帖我是来测试一下的");

form_data[5] = new NVPair("parseurloff","");
form_data[6] =new NVPair("smileyoff","");
form_data[7] =new NVPair("bbcodeoff","");
form_data[8] =new NVPair("isanonymous","");
form_data[9] =new NVPair("emailnotify","");
form_data[10] =new NVPair("sticktopic","");
form_data[11] =new NVPair("addtodigest","");
form_data[12] =new NVPair("addtoblog","");
form_data[13] =new NVPair("usesig",""); form_data[14] = new NVPair("message"," 最近没什么事做,想给理工的论坛增加一点人气,可是版主老是删我的贴,我都不知道为什么--");
form_data[15] = new NVPair("topicsubmit","发新话题");
//form_data[9]= new NVPair("wysiwygmode","所见即所得模式");

   wr.request(connection, "cs/post.php?action=newthread&fid=2&extra=page%3D1&topicsubmit=yes", "post", form_data);
   System.out.println("----提交完毕-----------------------------------------------------------------------------------------------------------------------------");
// 假如重定向,必须用该链接再次请求新的页面
// wr.request(connection, "index.html", "get", null);
}

public static void main(String[] args) {
try {
Beta f = new Beta();
f.login();
//f.send();
} catch (Exception e) {
e.printStackTrace();
} }
}class WebRequester {
private static InputStream istr = null;
private static HTTPConnection httpCon;
private static HTTPResponse rsp;
private static WebRequester instance; private WebRequester() {
} public static WebRequester getInstance() {
if (instance == null) {
instance = new WebRequester();
}
return instance;
} public String request(HTTPConnection connection, String pathName,
String method, NVPair form_data[]) {
try {

httpCon = connection;
if (method.toLowerCase().equals("get")) {
if (form_data != null)
rsp = httpCon.Get(pathName, form_data);
else
rsp = httpCon.Get(pathName);
} else {
if (form_data != null)
rsp = httpCon.Post(pathName, form_data);
else
rsp = httpCon.Post(pathName);
}
istr = rsp.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(istr));
String line;
StringBuffer result = new StringBuffer();
while ((line = reader.readLine()) != null) {
result.append(line + System.getProperty("line.separator"));
}
//System.out.println("result.toString()");
System.out.println(result.toString());
return result.toString();
} catch (Exception e) {
}
return null;
}}

解决方案 »

  1.   

    你要灌水啊!呵呵
    我给你点提示
    1 discuz 每个表单都有一个hashcode 的
    2 每个提交页面,都有一个 referer 头,表示页面来源Over, 不允许胡作非为。哈哈!
      

  2.   

    LZ,HTTPCLIENT好用吗?偶用的是HTMLUNIT
      

  3.   

    具体怎么办呢?
    我这里也遇到这个问题了,还是解决不了,
    我的代码是             post = new PostMethod(url);
                 List <Header> headers = new ArrayList <Header>(); 
                 headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"));
                 headers.add(new Header("Referer",referer));
                 hc.getHostConfiguration().getParams().setParameter("http.default-headers", headers); 
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GB2312");       
                MultipartRequestEntity mrp=new MultipartRequestEntity(parts,post.getParams());
                post.setRequestEntity(mrp);
                post.getParams().setContentCharset("GB2312");
                post.setFollowRedirects(false);
                int retcode = hc.executeMethod(post);
      

  4.   

    我估计是这出的问题 
    form_data[0]=new NVPair("formhash","a4921434");
    formhash每次都是动态生成的,为的是防止用户重复提交。而你这只是固定也死了,解决方法可以通过动态取页面的formhash值然后进行提交。