httpclient 模拟登录发贴

此程序可以实现在成功登录,但不能发贴,请高手赐教!总是提示“您的请求来路不正确,无法提交。”

以下是程序

 

 

package duwei;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class FormLoginDemo
{
 static final String LOGON_SITE = "www.open520.cn";
 static final int LOGON_PORT = 80;
 
 public static void main(String[] args) throws Exception
 {
  Logining();
 }
 
 public static void Logining() throws Exception
 {
  HttpClient client = new HttpClient();
  client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
//  http://www.open520.cn/logging.php?action=login&username=tongkuang&password=tongkuang&loginsubmit=yes
  PostMethod post = new PostMethod("/logging.php");
  NameValuePair action = new NameValuePair("action", "login");
  NameValuePair name = new NameValuePair("username", "tongkuang");
  NameValuePair pass = new NameValuePair("password", "tongkuang");
  NameValuePair loginsubmit = new NameValuePair("loginsubmit", "yes");
  
  post.setRequestBody(new NameValuePair[] { action,name, pass,loginsubmit});
  int status = client.executeMethod(post);
  System.out.println("status:" + status);
  
//  获取第一次请求返回的头部
  Header[] heard = post.getResponseHeaders();
  System.out.println("====================heard======================");
        for(int i = 0;i <heard.length; i ++)
        {
          System.out.println(heard[i].getName()+"||"+heard[i].getValue());
        }
        System.out.println("====================heard======================");
             
  String response = new String(post.getResponseBodyAsString().getBytes("8859_1"));
  System.out.println(response);
  System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
  post.releaseConnection();
  
  // 查看cookie信息
//  CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
//  Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false,client.getState().getCookies());
//  if (cookies.length == 0)
//  {
//   System.out.println("None");
//  }
//  else
//  {
//   for (int i = 0; i < cookies.length; i++)
//   {
//    System.out.println(cookies[i].toString());
//   }
//  }
  
  client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
  PostMethod post3 = new PostMethod("/pm.php");
  NameValuePair action3 = new NameValuePair("action", "send");
  NameValuePair pmsubmit = new NameValuePair("pmsubmit", "yes");
  NameValuePair infloat = new NameValuePair("infloat", "yes");
  NameValuePair sendnew = new NameValuePair("sendnew", "yes");
  NameValuePair msgto = new NameValuePair("msgto", "ww123456511");
  NameValuePair message = new NameValuePair("message", "nihao");
  NameValuePair formhash = new NameValuePair("formhash", "07d48a15");
  NameValuePair archives = new NameValuePair("archives", "http://www.open520.cn/archiver/");
  NameValuePair hashcode = new NameValuePair("hashcode", "hashcode");
//  NameValuePair referer = new NameValuePair("referer", "referer");
//  NameValuePair cook = new NameValuePair("jsessionid", "c3caiaVbq6shG%2BAKf%2FA%2FNG%2B5XnLriXoT0iyUmWq6eh9V4W3uyzSQEdn%2BauQumFkI2UPYAe4ulVvunlc32ELHU%2FeNZA");
  
//  post2.setRequestHeader("archives", "http://www.open520.cn/archiver/");
  post3.setRequestBody(new NameValuePair[] { action3,pmsubmit,infloat,sendnew,msgto,message,formhash,archives,hashcode});
  int status3 = client.executeMethod(post3);
  System.out.println("post3.getStatusCode():" + post3.getStatusCode());
  System.out.println("status3:" + status3);
  System.out.println("====================heard======================");
        for(int i = 0;i <heard.length; i ++)
        {
          System.out.println(heard[i].getName()+"||"+heard[i].getValue());
        }
        System.out.println("====================heard======================");
  String response3 = new String(post3.getResponseBodyAsString().getBytes("8859_1"));
  System.out.println(response3);
  post3.releaseConnection();
 }
}

 

 

 

 

 

 

 

 

以下是请求运行后的日志:

 

status:200
====================heard======================
Proxy-Connection||close
Connection||close
Via||1.1 69FWQ
Date||Tue, 26 Jan 2010 09:52:53 GMT
Content-type||text/html
Server||Microsoft-IIS/6.0
X-Powered-By||ASP.NET
X-Powered-By||PHP/5.2.1
Set-Cookie||ur0_sid=79FWgB; expires=Tue, 02-Feb-2010 09:52:53 GMT; path=/; httponly
Set-Cookie||ur0_cookietime=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_auth=fa78zPKzh6pq9YLjOZd%2FPYdpumdFpdcZxO0RNV07zjekK1nd4QObsDxGOHH%2Fu8TXLrcccY4yXKfmdkNdpjS7CmM9Rg; path=/; httponly
Set-Cookie||ur0_loginuser=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_activationauth=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_pmnum=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
====================heard======================
2010-1-26 17:55:04 org.apache.commons.httpclient.HttpMethodBase getResponseBody
警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title> 奥鹏作业答案-奥鹏在线作业答案交流网  </title>
<meta name="keywords" content="" />
<meta name="description" content=" 奥鹏作业答案-奥鹏在线作业答案交流网  - Discuz! Board" />
<meta name="generator" content="Discuz! 7.2" />
<meta name="author" content="Discuz! Team and Comsenz UI Team" />
<meta name="copyright" content="2001-2009 Comsenz Inc." />
<meta name="MSSmartTagsPreventParsing" content="True" />
<meta http-equiv="MSThemeCompatible" content="Yes" />
<meta http-equiv="x-ua-compatible" content="ie=7" />
<link rel="archives" title="奥鹏作业答案-奥鹏在线作业答案交流网" href="http://www.open520.cn/archiver/" />
<link href="/qqkefu/images/qq.css" rel="stylesheet" type="text/css" />
<script language='javascript' src='/qqkefu/ServiceQQ.js' type='text/javascript' charset='gbk'></script>
<link rel="stylesheet" type="text/css" href="forumdata/cache/style_2_common.css?vV7" />
<script type="text/javascript">var STYLEID = '2', IMGDIR = 'images/default', VERHASH = 'vV7', charset = 'gbk', discuz_uid = 2421, cookiedomain = '', cookiepath = '/', attackevasive = '0', disallowfloat = 'newthread|reply', creditnotice = '1|威望|,2|学习币|,3|信用|', gid = 0, fid = parseInt('0'), tid = parseInt('0')</script>
<script src="forumdata/cache/common.js?vV7" type="text/javascript"></script>
</head>

<body id="logging" οnkeydοwn="if(event.keyCode==27) return false;">

<div id="append_parent"></div><div id="ajaxwaitid"></div>

<div id="header">
<div class="wrap s_clear">
<h2><a href="index.php" title="奥鹏作业答案-奥鹏在线作业答案交流网"><img src="templates/fervor/images/logo.gif" alt="奥鹏在线作业答案网" border="0" /></a></h2>
<div id="umenu">
<cite><a href="space.php?uid=2421" class="noborder">tongkuang</a></cite>
<span class="pipe">|</span>
<a id="myprompt" href="notice.php" >提醒</a>
<span id="myprompt_check"></span>
<a href="pm.php" id="pm_ntc" target="_blank">短消息</a>
<a id="task_ntc" href="task.php" target="_blank">论坛任务</a>

<span class="pipe">|</span>
<a href="memcp.php">个人中心</a>
<a href="logging.php?action=logout&amp;formhash=a4d29971">退出</a>
</div>
<div id="ad_headerbanner"></div>
<div id="menu">
<ul>
<li class="menu_1"><a href="index.php" hidefocus="true" id="mn_index">论坛</a></li><li class="menu_6"><a href="memcp.php?action=credits" hidefocus="true" target="_blank" id="mn_memcp_1" style="font-weight: bold;color: red">学习币充值</a></li><li class="menu_2"><a href="search.php" hidefocus="true" id="mn_search">搜索</a></li><li class="menu_4"><a href="faq.php" hidefocus="true" id="mn_faq">帮助</a></li><li class="menu_5"><a href="misc.php?action=nav" hidefocus="true" οnclick="showWindow('nav', this.href);return false;">导航</a></li></ul>
<script type="text/javascript">
var currentMenu = $('mn_') ? $('mn_') : $('mn_index');
currentMenu.parentNode.className = 'current';
</script>
</div>
</div>
<div id="myprompt_menu" style="display:none" class="promptmenu">
<div class="promptcontent">
<ul class="s_clear"><li style="display:none"><a id="prompt_pm" href="pm.php?filter=newpm" target="_blank">私人消息 (0)</a></li><li style="display:none"><a id="prompt_announcepm" href="pm.php?filter=announcepm" target="_blank">公共消息 (0)</a></li><li style="display:none"><a id="prompt_task" href="task.php?item=doing" target="_blank">论坛任务 (0)</a></li><li style="display:none"><a id="prompt_systempm" href="notice.php?filter=systempm" target="_blank">系统消息 (0)</a></li><li style="display:none"><a id="prompt_friend" href="notice.php?filter=friend" target="_blank">好友消息 (0)</a></li><li style="display:none"><a id="prompt_threads" href="notice.php?filter=threads" target="_blank">帖子消息 (0)</a></li></ul>
</div>
</div>
</div>
<div id="nav"><a href="index.php">奥鹏作业答案-奥鹏在线作业答案交流网</a> &raquo; 提示信息</div>

<div id="wrap" class="wrap s_clear"><div class="main"><div class="content nofloat">
<div class="fcontent alert_win">
<h3 class="float_ctrl"><em>奥鹏作业答案-奥鹏在线作业答案交流网 提示信息</em></h3>
<hr class="shadowline" />
<div class="postbox"><div class="alert_info">
<p>欢迎您回来,tongkuang。现在将转入登录前页面。  </p>
</div></div>
</div>
</div></div></div>
</div><div id="ad_footerbanner1"></div><div id="ad_footerbanner2"></div><div id="ad_footerbanner3"></div>

<div id="footer">
<div class="wrap s_clear">
<div id="footlink">
<p>
<strong><a href="http://www.pen520.cn" target="_blank">学习百分百</a></strong>
( <a href="http://www.miibeian.gov.cn/" target="_blank">辽ICP备10000016号</a>)<span class="pipe">|</span><a href="mailto:786036802@qq.com">联系我们</a>
<span class="pipe">|</span><a href="archiver/" target="_blank">Archiver</a><span class="pipe">|</span><a href="wap/" target="_blank">WAP</a></p>
<p class="smalltext">
GMT+8, 2010-1-26 17:52, <span id="debuginfo">Processed in 0.031494 second(s), 4 queries</span>.
</p>
</div>
<div id="rightinfo">
<p>Powered by <strong><a href="http://www.discuz.net" target="_blank">Discuz!</a></strong> <em>7.2</em></p>
<p class="smalltext">&copy; 2001-2009 <a href="http://www.comsenz.com" target="_blank">Comsenz Inc.</a></p>
</div></div>
</div>
</body>
</html>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
post3.getStatusCode():200
status3:200
====================heard======================
Proxy-Connection||close
Connection||close
Via||1.1 69FWQ
Date||Tue, 26 Jan 2010 09:52:53 GMT
Content-type||text/html
Server||Microsoft-IIS/6.0
X-Powered-By||ASP.NET
X-Powered-By||PHP/5.2.1
Set-Cookie||ur0_sid=79FWgB; expires=Tue, 02-Feb-2010 09:52:53 GMT; path=/; httponly
Set-Cookie||ur0_cookietime=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_auth=fa78zPKzh6pq9YLjOZd%2FPYdpumdFpdcZxO0RNV07zjekK1nd4QObsDxGOHH%2Fu8TXLrcccY4yXKfmdkNdpjS7CmM9Rg; path=/; httponly
Set-Cookie||ur0_loginuser=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_activationauth=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
Set-Cookie||ur0_pmnum=deleted; expires=Mon, 26-Jan-2009 09:52:52 GMT; path=/
====================heard======================
2010-1-26 17:55:05 org.apache.commons.httpclient.HttpMethodBase getResponseBody
警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title> 奥鹏作业答案-奥鹏在线作业答案交流网  </title>
<meta name="keywords" content="" />
<meta name="description" content=" 奥鹏作业答案-奥鹏在线作业答案交流网  - Discuz! Board" />
<meta name="generator" content="Discuz! 7.2" />
<meta name="author" content="Discuz! Team and Comsenz UI Team" />
<meta name="copyright" content="2001-2009 Comsenz Inc." />
<meta name="MSSmartTagsPreventParsing" content="True" />
<meta http-equiv="MSThemeCompatible" content="Yes" />
<meta http-equiv="x-ua-compatible" content="ie=7" />
<link rel="archives" title="奥鹏作业答案-奥鹏在线作业答案交流网" href="http://www.open520.cn/archiver/" />
<link href="/qqkefu/images/qq.css" rel="stylesheet" type="text/css" />
<script language='javascript' src='/qqkefu/ServiceQQ.js' type='text/javascript' charset='gbk'></script>
<link rel="stylesheet" type="text/css" href="forumdata/cache/style_2_common.css?vV7" />
<script type="text/javascript">var STYLEID = '2', IMGDIR = 'images/default', VERHASH = 'vV7', charset = 'gbk', discuz_uid = 2421, cookiedomain = '', cookiepath = '/', attackevasive = '0', disallowfloat = 'newthread|reply', creditnotice = '1|威望|,2|学习币|,3|信用|', gid = 0, fid = parseInt('0'), tid = parseInt('0')</script>
<script src="forumdata/cache/common.js?vV7" type="text/javascript"></script>
</head>

<body id="pm" οnkeydοwn="if(event.keyCode==27) return false;">

<div id="append_parent"></div><div id="ajaxwaitid"></div>

<div id="header">
<div class="wrap s_clear">
<h2><a href="index.php" title="奥鹏作业答案-奥鹏在线作业答案交流网"><img src="templates/fervor/images/logo.gif" alt="奥鹏在线作业答案网" border="0" /></a></h2>
<div id="umenu">
<cite><a href="space.php?uid=2421" class="noborder">tongkuang</a></cite>
<span class="pipe">|</span>
<a id="myprompt" href="notice.php" >提醒</a>
<span id="myprompt_check"></span>
<a href="pm.php" id="pm_ntc" target="_blank">短消息</a>
<a id="task_ntc" href="task.php" target="_blank">论坛任务</a>

<span class="pipe">|</span>
<a href="memcp.php">个人中心</a>
<a href="logging.php?action=logout&amp;formhash=5f245ca7">退出</a>
</div>
<div id="ad_headerbanner"></div>
<div id="menu">
<ul>
<li class="menu_1"><a href="index.php" hidefocus="true" id="mn_index">论坛</a></li><li class="menu_6"><a href="memcp.php?action=credits" hidefocus="true" target="_blank" id="mn_memcp_1" style="font-weight: bold;color: red">学习币充值</a></li><li class="menu_2"><a href="search.php" hidefocus="true" id="mn_search">搜索</a></li><li class="menu_4"><a href="faq.php" hidefocus="true" id="mn_faq">帮助</a></li><li class="menu_5"><a href="misc.php?action=nav" hidefocus="true" οnclick="showWindow('nav', this.href);return false;">导航</a></li></ul>
<script type="text/javascript">
var currentMenu = $('mn_') ? $('mn_') : $('mn_index');
currentMenu.parentNode.className = 'current';
</script>
</div>
</div>
<div id="myprompt_menu" style="display:none" class="promptmenu">
<div class="promptcontent">
<ul class="s_clear"><li style="display:none"><a id="prompt_pm" href="pm.php?filter=newpm" target="_blank">私人消息 (0)</a></li><li style="display:none"><a id="prompt_announcepm" href="pm.php?filter=announcepm" target="_blank">公共消息 (0)</a></li><li style="display:none"><a id="prompt_task" href="task.php?item=doing" target="_blank">论坛任务 (0)</a></li><li style="display:none"><a id="prompt_systempm" href="notice.php?filter=systempm" target="_blank">系统消息 (0)</a></li><li style="display:none"><a id="prompt_friend" href="notice.php?filter=friend" target="_blank">好友消息 (0)</a></li><li style="display:none"><a id="prompt_threads" href="notice.php?filter=threads" target="_blank">帖子消息 (0)</a></li></ul>
</div>
</div>
</div>
<div id="nav"><a href="index.php">奥鹏作业答案-奥鹏在线作业答案交流网</a> &raquo; 提示信息</div>

<div id="wrap" class="wrap s_clear"><div class="main"><div class="content nofloat">
<div class="fcontent alert_win">
<h3 class="float_ctrl"><em>奥鹏作业答案-奥鹏在线作业答案交流网 提示信息</em></h3>
<hr class="shadowline" />
<div class="postbox"><div class="alert_info">
<p>您的请求来路不正确,无法提交。</p>
</div></div>
</div>
</div></div></div>
</div><div id="ad_footerbanner1"></div><div id="ad_footerbanner2"></div><div id="ad_footerbanner3"></div>

<div id="footer">
<div class="wrap s_clear">
<div id="footlink">
<p>
<strong><a href="http://www.pen520.cn" target="_blank">学习百分百</a></strong>
( <a href="http://www.miibeian.gov.cn/" target="_blank">辽ICP备10000016号</a>)<span class="pipe">|</span><a href="mailto:786036802@qq.com">联系我们</a>
<span class="pipe">|</span><a href="archiver/" target="_blank">Archiver</a><span class="pipe">|</span><a href="wap/" target="_blank">WAP</a></p>
<p class="smalltext">
GMT+8, 2010-1-26 17:52, <span id="debuginfo">Processed in 0.020210 second(s), 1 queries</span>.
</p>
</div>
<div id="rightinfo">
<p>Powered by <strong><a href="http://www.discuz.net" target="_blank">Discuz!</a></strong> <em>7.2</em></p>
<p class="smalltext">&copy; 2001-2009 <a href="http://www.comsenz.com" target="_blank">Comsenz Inc.</a></p>
</div></div>
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值