gpt4 book ai didi

java - 在 Java 中使用 HTTP post 来模拟网络浏览器

转载 作者:可可西里 更新时间:2023-11-01 16:30:43 24 4
gpt4 key购买 nike

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

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

?>

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


The Problem :

我需要使用 java(最好是 HttpURLConnection)以编程方式发布数据并从服务器获取响应消息..这将只是“wecome to 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;
}
}

最佳答案

您调用了错误的资源!在 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'>

关于java - 在 Java 中使用 HTTP post 来模拟网络浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com