わかりました、これが簡単なセットアップです...
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>
ようこそ.php
<?php
if($_POST['usernamebox'] == 'BLOB' && $_POST['passwordbox'] == 'password')
{
echo "welcome to BLOB!";
}
else
{
header ('Location:index.html');
}
?>
セットアップ (ローカルホストにあります) は正常に動作し、「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;
}
}