BLOB
BLOB

Reputation: 374

using HTTP post in Java to simulate web browser

Ok, Here is the simple setup...

index.html

<html>   
<p> Login to Blob </p>
<form action='welcome.php' method='post'>
      <div>
      <p> Username: </p>
     <input type='text' name='usernamebox' id='inputtext'/>        
     </div>
     <div>
     <p> Password: </p>
     <input type="text" name="passwordbox" id='inputpass'/>
     </div>
     <div>
     <input type='submit' value='submit' id='button'/>
     </div>         
</form>

</html>

welcome.php

<?php

    if($_POST['usernamebox'] == 'BLOB' && $_POST['passwordbox'] == 'password')
    {
        echo "welcome to BLOB!";
    }

    else
    {
        header ('Location:index.html');
    }

?>

The setup (which is in localhost), works fine and I see "welcome to BLOB!" message only when i set the username field as 'BLOB' and password field as 'password'.


The Problem :

I need to use java (preferably HttpURLConnection) to post the data programmatically and get the response message from the server.. which will just be "wecome to BLOB!"..

I have tried this code but it gives me back the html of index.html but not the response from welcome.php...

import java.net.*;
import java.io.*; 

public class DateServer
{
    private static final String TARGET_URL = "http://localhost/myfiles/index.html";

    public static void main(String[] args)
    {
        String response = readResponse(doHttpPost(TARGET_URL, "usernamebox=BLOB&passwordbox=password"));

        System.out.println("Response : \n" + response);
    }

    public static HttpURLConnection doHttpPost(String targetUrl, String urlEncodedContent)
    {
        try
        {
            HttpURLConnection urlConnection = (HttpURLConnection)(new URL(targetUrl).openConnection());
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");

            HttpURLConnection.setFollowRedirects(true);
            urlConnection.setRequestMethod("POST");

            DataOutputStream dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());

            // throws IOException
            dataOutputStream.writeBytes(urlEncodedContent);
            dataOutputStream.flush();
            dataOutputStream.close();

            return urlConnection;           

        }

        catch (IOException e)
        {
            e.printStackTrace();
        }

        return null; 
    }

    private static String readResponse(HttpURLConnection urlConnection)
    {
        BufferedReader bufferedReader = null;
        try
        {

            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String responeLine;

            StringBuilder response = new StringBuilder();

            while ((responeLine = bufferedReader.readLine()) != null)
            {
                response.append(responeLine);
            }

            return response.toString();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally  // closing stream
        {
            if (bufferedReader != null)
            {   try
                {                   
                    bufferedReader.close();                   
                }
                catch (IOException e)   
                {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

Upvotes: 2

Views: 6185

Answers (1)

El Hocko
El Hocko

Reputation: 2606

you are calling the wrong resource! In java, the request must go to the php script, not the html

change

private static final String TARGET_URL = "http://localhost/myfiles/index.html";

into

private static final String TARGET_URL = "http://localhost/myfiles/welcome.php";

because this is what happens in the index.php

the form passes the form-data to the welcome.php

<form action='welcome.php' method='post'>

Upvotes: 3

Related Questions