简体   繁体   中英

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

I have to do this as my assignment which is as: Write a java program to get a html page from server then fill out the form using the java program and then send it back to server.

For this I had written the following piece of code:

    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){

                    }
            }
    }

And here is the html file:

        <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.

I would strongly recommend looking at using JSoup (http://jsoup.org/). It's basically jquery for java. You can get input fields really easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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