I'm currently trying to create a program that will log into the web page given to it if supplied with the user name and password.

but the problem im having is sending the POST request to the website and then retreiveing the URL it sends back to load in a web browser.


        URL url;
        URLConnection urlConn;
        DataOutputStream printout;
        DataInputStream input;
        // URL 
        url = new URL("url.com");

        // URL connection channel.
        urlConn = url.openConnection();


        urlConn.setRequestProperty("User", "user");
        urlConn.setRequestProperty("Password", "password");
        urlConn.setRequestProperty("Submit", "Send");

        printout = new DataOutputStream(urlConn.getOutputStream());

        String content =
                "name=" + URLEncoder.encode("Buford Early")
                + "&email=" + URLEncoder.encode("buford@known-space.com");

        input = new DataInputStream(urlConn.getInputStream());
        String str;
        while (null != ((str = input.readLine()))) {
            System.out.println("" + (str + "\n"));
        catch(Exception e)


From what i understand the first parameter in setRequestPropety is the name of the field. the second is what you wish to send.

So the 3rd one i send should submit the data. but im also not sure how i retrieve the data sent back.

the data i receive back is the html code of the webpage i load not the web address of the webpage i want to log into.

any help would be appreciated.

Recommended Answers

All 2 Replies

Try reading through this.

Hi masijade,

I've sat and read though that thread a couple of times but im struggerling to understand the final code that is used.

I think i'm also encountering the same problem he had where i might need to use a packet sniffer to get a few things that are hidden when sent with the user login and password. because the weblink that is returned when done manually does not contain anything that indicates someone has logged in. do i need to set something in the cookies with the data i send??


i tried the same code im using but rather then pointing it to my desired website i created a test webpage with just 2 text boxs and a button. but when i run the program i receive nothing back. where normaly i would recieve the current HTML code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.