在Java中模拟单击提交按钮

在Java中模拟单击提交按钮,java,http-request,Java,Http Request,我正在用Java编写程序,我想填写表单的指定字段,模拟提交点击,从而获得结果页面。我正在url上测试我的想法http://stu21.kntu.ac.ir/Login.aspx有两个字段txtsername和txtpassword。我正在尝试下面的代码,但它不会为我返回结果页。我怎么做?这个代码有什么错 DefaultHttpClient conn = new DefaultHttpClient(); conn = new DefaultHttpClient();

我正在用Java编写程序,我想填写表单的指定字段,模拟提交点击,从而获得结果页面。我正在url
上测试我的想法http://stu21.kntu.ac.ir/Login.aspx
有两个字段
txtsername
txtpassword
。我正在尝试下面的代码,但它不会为我返回结果页。我怎么做?这个代码有什么错

        DefaultHttpClient conn = new DefaultHttpClient();
        conn = new DefaultHttpClient();

        ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("txtusername", "8810103"));
        pairs.add(new BasicNameValuePair("txtpassword", "8810103"));

        HttpPost httpPost = new HttpPost("http://stu21.kntu.ac.ir/Login.aspx");
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,
                "UTF-8");

        httpPost.setHeader(
                "UserAgent",
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19");

        httpPost.setEntity(entity);
        HttpResponse response = conn.execute(httpPost);
        InputStream is = response.getEntity().getContent();
        RandomAccessFile raf = new RandomAccessFile(
                "/home/hossein/Desktop/random.aspx", "rw");
        raf.seek(0);

        int bytes = 0;
        byte[] buffer = new byte[1024];

        while ((bytes = is.read(buffer)) != -1)
            raf.write(buffer, 0, bytes);

        raf.close();
        is.close();
DefaultHttpClient conn=newdefaulthttpclient();
conn=新的DefaultHttpClient();
ArrayList pairs=新的ArrayList();
添加(新的BasicNameValuePair(“txtusername”、“8810103”);
添加(新的BasicNameValuePair(“txtpassword”、“8810103”);
HttpPost HttpPost=新的HttpPost(“http://stu21.kntu.ac.ir/Login.aspx");
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(成对,
“UTF-8”);
httpPost.setHeader(
“用户代理”,
“Mozilla/5.0(X11;Linux x86_64)AppleWebKit/535.19(KHTML,类似Gecko)Ubuntu/12.04 Chrome/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19”);
httpPost.setEntity(实体);
HttpResponse response=conn.execute(httpPost);
InputStream is=response.getEntity().getContent();
RandomAccessFile raf=新的RandomAccessFile(
“/home/hossein/Desktop/random.aspx”、“rw”);
raf.seek(0);
int字节=0;
字节[]缓冲区=新字节[1024];
而((字节=is.read(缓冲区))!=-1)
写入(缓冲区,0,字节);
raf.close();
is.close();
抱歉,如果我的问题与其他线程重复,我无法在其他线程上找到我的解决方案

提前谢谢:)

我想你需要。这里有一个很好的教程

请看下面的示例:

public void testGoodLogin() throws Exception {
    WebConversation     conversation = new WebConversation();
    WebRequest  request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );
    WebResponse response = conversation.getResponse( request );

    WebForm loginForm = response.getForms()[0];
    request = loginForm.getRequest();
    request.setParameter( "name", "master" );

    // "clicking the button" programatically
    response = conversation.getResponse( request );

    assertTrue( "Login not accepted", 
               response.getText().indexOf( "You made it!" ) != -1 );
    assertEquals( "Page title", "Top Secret", response.getTitle() );
} 
我相信您可以像这样进行测试。

我想您需要。这里有一个很好的教程

请看下面的示例:

public void testGoodLogin() throws Exception {
    WebConversation     conversation = new WebConversation();
    WebRequest  request = new GetMethodWebRequest( "http://www.meterware.com/servlet/TopSecret" );
    WebResponse response = conversation.getResponse( request );

    WebForm loginForm = response.getForms()[0];
    request = loginForm.getRequest();
    request.setParameter( "name", "master" );

    // "clicking the button" programatically
    response = conversation.getResponse( request );

    assertTrue( "Login not accepted", 
               response.getText().indexOf( "You made it!" ) != -1 );
    assertEquals( "Page title", "Top Secret", response.getTitle() );
} 

我相信您可以像这样进行测试。

尝试将其直接指向login.aspx页面。也许它也不是站点的默认页面,它应该在try-catch块中。打印错误堆栈,看看它说什么我测试了你的答案,它也不起作用:-你怎么知道它不起作用?它有什么作用?或者不这样做?尝试将其直接指向login.aspx页面。也许它也不是站点的默认页面,它应该在try-catch块中。打印错误堆栈,看看它说什么我测试了你的答案,它也不起作用:-你怎么知道它不起作用?它有什么作用?还是不行?