4
回答
用HttpClient3 对discuz论坛发帖
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

受朋友之托,帮她弄个盖楼(发帖的东西),但是弄了很久没有成功,请高手帮忙看下

package com.dbasy.http.simulate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/*
 *@auth         DBASY
 *@param mail:  whqdh_815@163.com
 *@param date:  Jul 22, 2010
 */
public class SimulateZH {
 private Cookie[] cookies = new Cookie[0]; // 维护Cookies
 private static HttpClient client = null;

 private SimulateZH() {
  client = new HttpClient();
   client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  // http 请求默认端口为80
  client.getHostConfiguration()
    .setHost("zzhzbbs.zjol.com.cn", 80, "http");

 }

 public static HttpClient getInstance() {
  if (client == null) {
   new SimulateZH();
  }
  return client;

 }

 

 public Cookie[] getCookies() {
  return cookies;
 }

 public void setCookies(Cookie[] cookies) {
  this.cookies = cookies;
 }

 /*
  *
  * cookie  *
  * @return
  *
  */

 public void addCookie(Cookie c) {
  if (cookies != null && cookies.length > 0) {
   Cookie[] others = new Cookie[cookies.length + 1];
   System.arraycopy(cookies, 0, others, 0, cookies.length);
   others[others.length - 1] = c;
   cookies = others;
  } else {
   cookies = new Cookie[1];
   cookies[0] = c;
  }
 }

 // 查看cookie信息
 public void printCookies() {
  System.out.println(" ---------------Cookie---------------- ");
  if (cookies != null) {
   for (Cookie c : cookies) {
    System.out.println(c.getName() + " : " + c.getValue());
   }
  } else {
   System.out.println(" 没有设置Cookies ");
  }
  System.out.println(" ---------------Cookie---------------- ");
 }

 /*
  * 登陆系统
  *
  */
 private void login() {
  byte[] loginResponse = null;

  PostMethod post = new PostMethod(
    "/logging.php?action=login&loginsubmit=yes&inajax=1");
  //用户名和密码请去注册个 地址为 http://zzhzbbs.zjol.com.cn/register.php?referer=http%3A//zzhzbbs.zjol.com.cn/thread-3382377-1-1.html
  NameValuePair username = new NameValuePair("username", "");
  NameValuePair password = new NameValuePair("password", "");
  NameValuePair formhash = new NameValuePair("formhash", "3f3bba52");
  NameValuePair referer = new NameValuePair(
    "referer",
    "http%3A%2F%2Fzzhzbbs.zjol.com.cn%2F%2Fviewthread.php%3Ftid%3D3374820%26extra%3D%26page%3D614");
  NameValuePair next = new NameValuePair("next", "1");
  NameValuePair loginsubmit = new NameValuePair("loginsubmit",
    "%B5%C7%C2%BD");

  post.setRequestBody(new NameValuePair[] { username, password, formhash,
    referer, next, loginsubmit });
  try {
   client.executeMethod(post);
   loginResponse = post.getResponseBody();
   post.releaseConnection();
   cookies = client.getState().getCookies();
   //打印cookie信息
   printCookies();
   System.out.println(new String(loginResponse, "GBK"));
   //解析登陆成功后信息,这里不去请求的话会发现报请求不正确错误
   parseLoginResponse(new String(loginResponse, "GBK"));

  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 /*
  * 登陆成功后处理返回信息
  *
  */
 private void parseLoginResponse(String loginResp) {
  // System.out.println(new String(method.getResponseBody(), "GBK"));
  StringUtil su = new StringUtil();
  List getList = su.searchStrArray(loginResp, "src=\"", "\"");
  List locationList = su.searchStrArray(loginResp,
    "window.location.href='", "'");
  // 发起get请求
  for (int i = 0; i < getList.size(); i++) {
   System.out.println("发起get请求 href=" + getList.get(i));
   doGetMethod(getList.get(i).toString());
  }
  // 发起location跳转请求
//  for (int i = 0; i < locationList.size(); i++) {
//   System.out.println("发起location请求 href=" + locationList.get(i));
//   doPostMethodNoValue(locationList.get(i).toString());
//  }

 }

 /*
  *
  * 使用POST方式提交内容
  *
  * @return
  *
  */

 private void submitPostMethod() {

  // POST
  // /post.php?action=reply&fid=1437&tid=3374820&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1
  // HTTP/1.1
  // Accept: application/x-shockwave-flash, image/gif, image/x-xbitmap,
  // image/jpeg, image/pjpeg, application/x-silverlight,
  // application/vnd.ms-excel, application/vnd.ms-powerpoint,
  // application/msword, */*
  // Referer: http://zzhzbbs.zjol.com.cn/thread-3374820-1-1.html
  // Accept-Language: zh-cn
  // Content-Type: application/x-www-form-urlencoded
  // Accept-Encoding: gzip, deflate
  // User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;
  // .NET CLR 2.0.50727; InfoPath.2)
  // Host: zzhzbbs.zjol.com.cn
  // Content-Length: 49
  // Connection: Keep-Alive
  // Cache-Control: no-cache
  // Cookie: xsxscdb_cookietime=2592000; xsxscdb_loginuser=dbasy;
  // xsxscdb_activationauth=1e99gDbIkpP2EWHA2pdrlBpvKrvyiPjb%2FAqb0D5MQ3AkzA;
  // visitor_user_id=1279802913413;
  // lastVisitTime=Fri%20Jul%2023%2013%3A22%3A12%20UTC+0800%202010;
  // smile=8D1; ZZHZ_oldtopics=D3382377D3374820D; ZZHZ_sid=Wz4J67;
  // ZZHZ_visitedfid=1437D84D1326D2; ZZHZ_fid1437=1279862020;
  // ZZHZ_auth=02b8oSzOYIuKQh9cgUavtOBuUwM8OzgfaZcOUJ1iMMRHFLFUSeBHB11VZBKOpvrbl3bKGRaEK63jCK2n3QMfVIDEKw8lIw
  //
  // formhash=d71faeca&subject=&usesig=0&message=34343

  PostMethod post = new PostMethod(
    "/post.php?action=reply&fid=1437&tid=3374820&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1");

  NameValuePair formhash = new NameValuePair("formhash", "d71faeca");
  NameValuePair subject = new NameValuePair("subject", "");
  NameValuePair usesig = new NameValuePair("usesig", "2");
  NameValuePair message = new NameValuePair("message", "88888888");

  NameValuePair referer = new NameValuePair(
    "referer",
    "http%3A%2F%2Fzzhzbbs.zjol.com.cn%2F%2Fviewthread.php%3Ftid%3D3374820%26extra%3D%26page%3D614");

  post.setRequestBody(new NameValuePair[] { formhash, subject, usesig,
    message, referer });
  try {
   // post.addRequestHeader("Cookie",
   // client.getState().getCookies().toString());
   printCookies();
   client.executeMethod(post);
   cookies = client.getState().getCookies();
   printCookies();
   System.out.println("################################");
   System.out.println(new String(post.getResponseBody(), "GBK"));
   post.releaseConnection();
  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 /*
  *
  * 使用POST方式提交内容
  *
  * @return
  *
  */

 private void doPostMethodNoValue(String url) {

  PostMethod post = new PostMethod(url);
  try {
   client.executeMethod(post);
   cookies = client.getState().getCookies();
   System.out.println(post.getResponseBodyAsString());
   printCookies();

   post.releaseConnection();
  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 /*
  *
  * 使用get方式提交内容
  *
  * @return
  *
  */

 private void doGetMethod(String url) {

  GetMethod getMethod = new GetMethod(url);

  getMethod.getParams().setCookiePolicy(
    CookiePolicy.BROWSER_COMPATIBILITY);

  // method.setRequestHeader("Cookie", cookies);

  getMethod.setFollowRedirects(false);
  try {
   client.executeMethod(getMethod);
   cookies = client.getState().getCookies();
   printCookies();
   getMethod.releaseConnection();
   // System.out.println(new String(getMethod.getResponseBody(),
   // "GBK"));
  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public void doEvent() {

  System.out.println("########登陆住杭网#################");
  client = SimulateZH.getInstance();
  login();
  submitPostMethod();

 }

 public static void main(String[] args) {

  SimulateZH szh = new SimulateZH();
  szh.doEvent();
 }

}
执行结果:

<?xml version="1.0" encoding="gbk"?><root><![CDATA[您所在的用户组(游客)无法进行此操作。<script type="text/javascript" reload="1">
if(typeof messagehandle_fastpost =='function') {messagehandle_fastpost('NOPERM', '您所在的用户组(游客)无法进行此操作。');}
</script>]]></root>

 

对于discuz论坛个人查了些资料,需要的参数有 板块id  回帖id 还有就是formhash ,但是还是没有成功,希望好心人 牛人帮助?

举报
草木溪
发帖于7年前 4回/1K+阅
顶部