使用Java中的HTTP post来模拟Web浏览器

时间:2013-02-19 02:18:01

标签: java php http

好的,这是简单的设置......

  

的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');
    }

?>

设置(在localhost中),工作正常,我看到“欢迎来到BLOB!”仅当我将用户名字段设置为“BLOB”并将密码字段设置为“密码”时才会显示消息。


  

问题:

我需要使用java(最好是HttpURLConnection)以编程方式发布数据并从服务器获取响应消息..这将只是“我们来到BLOB!”..

我已经尝试过这段代码了,但是它回复了index.html的html而不是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;
    }
}

1 个答案:

答案 0 :(得分:3)

你正在拨打错误的资源! 在java中,请求必须转到php脚本,而不是html

更改

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

进入

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

因为这是index.php

中发生的事情

表单将表单数据传递给welcome.php

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