2

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

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

欢迎.php

<?php

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

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

?>

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


问题 :

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

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

1 回答 1

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'>
于 2013-02-19T02:39:04.137 回答