当前位置:编程学习 > JAVA >>

java 实现模拟 登陆网站、、、、高手看看哪错了!!

package on.dataconnection;
import java.io.IOException;
import java.net.MalformedURLException;

import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class LoginTest {
 /**
  * form的get方法登录
  * @throws MalformedURLException
  * @throws IOException
  * @throws SAXException
  *///2311222
public void getMethod() throws MalformedURLException, IOException, SAXException{
//  建立一个WebConversation实例 
  WebConversation wc = new WebConversation(); 
//  向指定的URL发出请求 
  WebRequest req = new GetMethodWebRequest( " http://www.17gude.com/ " ); 
//  给请求加上参数 
  WebResponse response=wc.getResponse(req);
  WebForm form = response.getForms()[0];
  form.setParameter("UserLoginName","你舅舅说"); 
  form.setParameter("UserLoginPass","chenying147"); 
  req=form.getRequest();
  response =  wc.getResponse(req);
  //assertTrue("转到'zsonline'【suibian】用户首页失败!",response.getText().indexOf("用户测试用户_zsonline,您好!") != -1);     }

//  获取响应对象 
  //WebResponse resp = wc.getResponse( req ); 
  
//  用getText方法获取相应的全部内容 
//  用System.out.println将获取的内容打印在控制台上 
  System.out.println( response.getText() ); 
 


 }
-----------------------------------------------------------------
报错如下:::
----------------------------------------------------------------
使用Post方式向服务器发送数据,然后获取网页内容:
Rhino classes (js.jar) not found - Javascript disabled
Exception in thread "main" java.lang.RuntimeException: No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath
at com.meterware.httpunit.parsing.HTMLParserFactory.getHTMLParser(HTMLParserFactory.java:92)
at com.meterware.httpunit.HTMLPage.parse(HTMLPage.java:272)
at com.meterware.httpunit.WebResponse.getReceivedPage(WebResponse.java:945)
at com.meterware.httpunit.WebResponse.getForms(WebResponse.java:294)
at on.dataconnection.LoginTest.postMethod(LoginTest.java:59)
at on.dataconnection.LoginTest.main(LoginTest.java:72)
--------------------编程问答-------------------- at on.dataconnection.LoginTest.postMethod(LoginTest.java:59)

你的postMethod方法呢? --------------------编程问答--------------------
引用 1 楼 dzr1990 的回复:
at on.dataconnection.LoginTest.postMethod(LoginTest.java:59)

你的postMethod方法呢?

package on.dataconnection;
import java.io.IOException;
import java.net.MalformedURLException;

import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

public class LoginTest {
 /**
  * form的get方法登录
  * @throws MalformedURLException
  * @throws IOException
  * @throws SAXException
  *///2311222
public void getMethod() throws MalformedURLException, IOException, SAXException{
//  建立一个WebConversation实例 
  WebConversation wc = new WebConversation(); 
//  向指定的URL发出请求 
  WebRequest req = new GetMethodWebRequest( " http://www.17gude.com/ " ); 
//  给请求加上参数 
  WebResponse response=wc.getResponse(req);
  WebForm form = response.getForms()[0];
  form.setParameter("UserLoginName","你舅舅说"); 
  form.setParameter("UserLoginPass","chenying147"); 
  req=form.getRequest();
  response =  wc.getResponse(req);
  //assertTrue("转到'zsonline'【suibian】用户首页失败!",response.getText().indexOf("用户测试用户_zsonline,您好!") != -1);     }

//  获取响应对象 
  //WebResponse resp = wc.getResponse( req ); 
  
//  用getText方法获取相应的全部内容 
//  用System.out.println将获取的内容打印在控制台上 
  System.out.println( response.getText() ); 
 


 }
 /**
  * form的post方式登录
  * @throws MalformedURLException
  * @throws IOException
  * @throws SAXException
  */
 public void postMethod() throws MalformedURLException, IOException, SAXException{
  System.out.println("使用Post方式向服务器发送数据,然后获取网页内容:"); 
//  建立一个WebConversation实例 
  WebConversation wc=new WebConversation();
  WebResponse responseLogin;
  WebRequest requestLogin;
  requestLogin = new PostMethodWebRequest(" http://www.17gude.com/ " );
  responseLogin  = wc.getResponse(requestLogin);
  System.out.println("使用Post方式向服务器发送数据,然后获取网页内容:1"); 
   WebForm form = responseLogin.getForms()[0];

   form.setParameter("UserLoginName","你舅舅说"); 
   form.setParameter("UserLoginPass","chenbin"); //此处需要填写真实密码
 
  requestLogin = form.getRequest();
  responseLogin =  wc.getResponse(requestLogin);
  //assertTrue("转到'zsonline'【suibian】用户首页失败!",responseLogin.getText().indexOf("用户测试用户_zsonline,您好!") != -1);     

System.out.println(responseLogin.getText());

 }
 
 public static void main(String[] args) throws Exception{
  LoginTest test = new LoginTest();
  test.postMethod();
 }

} --------------------编程问答-------------------- 没有接触过这个东西,只是弱弱的说一句,是不是有的包没有导进来啊,他叫你确认 nekoHTML.jar or Tidy.jar is in the in classpath

Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,