繁体   English   中英

使用Java程序获取页面,然后在填写后将其发送回

[英]get a page using java program and then send it back after filling out

我必须将其作为我的任务,这是:编写一个Java程序从服务器获取html页面,然后使用Java程序填写表单,然后将其发送回服务器。

为此,我编写了以下代码:

    class htmlPageFetch{
        public static void main(String[] args){
                try{
                        Socket s = new Socket("127.0.0.1", 80);
                        DataInputStream dIn = new DataInputStream(s.getInputStream());


        PrintWriter dOut = new PrintWriter(s.getOutputStream(), true);
                    dOut.println("GET /mytesting/justCheck.html HTTP/1.1\r\nHost:localhost\r\n\r\n");
                    boolean more_data = true;
                    String str;
                    while(more_data){


     str = dIn.readLine();
                                    if(str==null){
                                            //Now server has stopped sending data                                           //So now write again the inputs
//So i had written the following code but not working
    dOut.println("POST /mytesting/save.php HTTP/1.1\r\nHost:localhost\r\n\r\n");
    dOut.println("some=helloworld");
                                            more_data = false;
                                            continue;
                                    }
                                    System.out.println(str);
                            }
                    }catch(IOException e){

                    }
            }
    }

这是html文件:

        <html>
    <head>
    <title>Title goes here</title>
    </head>
    <body>
    <p>Hello world</p>
    <form action="save.php" method="post">
    Enter some thing here <input name="some"/>
    <br/>
    <input type="submit" value="Send"/>
    <input type="reset" value="Cancel"/>
    </form>
    </body>
    </html>

and the save.php just echo back the string entered.

So how to send back the filled form to the server back.

Thanks in Advance.

我强烈建议您使用JSoup(http://jsoup.org/)。 基本上,它是Java的jquery。 您可以非常轻松地获取输入字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM