我正在尝试向位于 Web 服务器上的 php 文件发送 POST 请求。目前我已成功发送请求,但标头未正确发送。
以下是我要发送的查询字符串:
lastName=Jones&title=Android+Game+Programming+2&price=22.99&isbn=9876543210123&year=2012&firstName=Joe&publisher=Android+Press
这就是我尝试将它们发送到服务器的方式:
if(method.equalsIgnoreCase("POST")){
//Write the http post request to web server
s.getOutputStream().write(("POST " + path + " HTTP/1.0\r\n").getBytes("ASCII"));
s.getOutputStream().write("Host: www.jdiadt.com\r\n\r\n".getBytes("ASCII"));
//Request Headers
String title = "title: "+request.getParameters().get("title") + "\r\n";
String firstName = "firstName: "+request.getParameters().get("firstName") + "\r\n";
String lastName = "lastName: " + request.getParameters().get("lastName") + "\r\n";
String isbn = "isbn: " + request.getParameters().get("isbn") + "\r\n";
String publisher = "publisher: " + request.getParameters().get("publisher") + "\r\n";
String year = "year: " + request.getParameters().get("year") + "\r\n";
String price = "price: " + request.getParameters().get("price") + "\r\n";
s.getOutputStream().write(title.getBytes("ASCII"));
s.getOutputStream().write(firstName.getBytes("ASCII"));
s.getOutputStream().write(lastName.getBytes("ASCII"));
s.getOutputStream().write(isbn.getBytes("ASCII"));
s.getOutputStream().write(publisher.getBytes("ASCII"));
s.getOutputStream().write(year.getBytes("ASCII"));
s.getOutputStream().write(price.getBytes("ASCII"));
//Blank line
String blankline = "\r\n";
s.getOutputStream().write(blankline.getBytes("ASCII"));
//Flush and wait for response...
s.getOutputStream().flush();
当我运行代码时,我从脚本中收到此通知,这使我相信我没有正确发送标头:
Notice: Undefined index: title in C:\wamp\www\bookstore\createBook.php on line 3
对于我尝试检索通过 POST 发送的变量的每一行,它都会给出上述错误。这是代码:
$title = $_POST['title'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$year = $_POST['year'];
$price = $_POST['price'];
关于可能出现什么问题的任何信息?
createBook.php
<?php
$title = $_POST['title'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$year = $_POST['year'];
$price = $_POST['price'];
try {
require_once 'BookDAO.php';
require_once 'Book.php';
$dao = new BookDAO();
$book = new Book(NULL, $title, $firstName, $lastName,
$publisher, $isbn, $year, $price);
$dao->insert($book);
$books = $dao->findAll();
if (count($books) > 0) {
echo '<table>';
echo '<tr>';
echo ' <th>Title</th>
<th>First name</th>
<th>Last name</th>
<th>Year</th>
<th>Price</th>
<th>Actions</th>';
echo '</tr>';
foreach ($books as $book) {
echo '<tr>';
echo '<td>' . $book->getTitle() . '</td>';
echo '<td>' . $book->getFirstName() . '</td>';
echo '<td>' . $book->getLastName() . '</td>';
echo '<td>' . $book->getYear() . '</td>';
echo '<td>' . $book->getPrice() . '</td>';
echo '<td>';
echo '<a href="editBookForm.php?id=' . $book->getId() . '">';
echo '<img src="images/edit20.png" alt="Edit Book" />';
echo '</a>';
echo '<a href="deleteBook.php?id=' . $book->getId() . '"';
echo ' onclick="return confirm(\'Are you sure you want to delete';
echo ' this book?\');">';
echo '<img src="images/delete20.png" alt="Delete Book" />';
echo '</a>';
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
else {
echo "<p>There are no books in the database.</p>";
}
echo '<p>';
echo '<a href="createBookForm.php">';
echo '<img src="images/new20.png" alt="New Book" /> New Book';
echo '</a>';
echo '</p>';
}
catch (PDOException $e) {
exit("Connection failed: " . $e->getMessage());
}
?>