2

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;
    }
}
6
  • I would recommend wireshark to debug this Commented Feb 19, 2013 at 2:21
  • But I think the problem is something simple.. something to do with redirects isn't it? Commented Feb 19, 2013 at 2:31
  • maybe username and password are passed the wrong way, therefore the phpscript goes into the else-part sending a redirecting header.. Commented Feb 19, 2013 at 2:34
  • what about echo $_POST['usernamebox'] and echo $_POST['passwordbox'] Commented Feb 19, 2013 at 2:35
  • just changed the else part to echo 'login failed' and saved it and ran it again.. still getting same index.html Commented Feb 19, 2013 at 2:36

1 Answer 1

3

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'>
Sign up to request clarification or add additional context in comments.

12 Comments

Just changed TARGET_URL to the one u said.. It worked! but it is not exactly simulating the browser is it? How to do it through index.html?
extra question... What does 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"); do?
The browser reads the form-data and generates a new request he knows the action (welcome.php), the method (post) and the post-parameter (the form-data) Then he technically does the same thing, but with a lot more code involved;)
you should consider reading something that gives you a basic understanding of http
Using the console OP could understand the URL, the kind of request and the parameters to send from Java side (using Apache HttpClient or another library)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.