为了发送用户名和密码,我正在使用以下内容:
public class post {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.example.com/practice.php");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("username", "user"));
params.add(new BasicNameValuePair("password", "hunter2"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
// do something useful
} finally {
instream.close();
}
}
}
}
我正在使用的PHP脚本用于收集信息,如下所示,它很简单,但目前仅用于测试。
<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo "username = $username<br>";
echo "password = $password<br>";
?>
我想知道有没有人能帮我指明正确的方向,让我能在php中接受来自Java的HTTP POST请求,或者告诉我是否正确地发送了POST请求,非常感谢任何帮助。