0

我正在尝试向位于 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());
}
?>
4

3 回答 3

1

您正在以 GET 类型(查询字符串)的方式设置参数,并尝试使用 POST 获取它们。您应该更改其中任何一个,以使它们相同。

于 2013-01-29T22:50:23.807 回答
1

我自己解决了这个问题。对于想知道我在这里做了什么的人来说,这是我的解决方案。

正如您在上面的代码中看到的那样,我在原始问题中包含我试图一个接一个地发送标题,这是完全错误的事情!

我检查了以下两个链接(特别是第二个链接),发现它们对解释 POST 请求结构非常有帮助。

http://net.tutsplus.com/tutorials/other/http-headers-for-dummies/

http://ruturajv.wordpress.com/2005/12/25/http-post-request/

然后我回到我的代码,对我构建发布请求的方式进行了以下更改:

//POST REQUEST
    if(method.equalsIgnoreCase("POST")){

        //CONSTRUCT REQUEST
        String blankline = "\r\n";
        String query = request.getQueryString();
        String length = String.valueOf(query.length());
        System.out.println(length);

        //Write the http post request to web server
        s.getOutputStream().write(("POST " + path + " HTTP/1.0" +"\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Host: localhost.com" + "\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Content-Type: application/x-www-form-urlencoded"+"\r\n").getBytes("ASCII"));
        s.getOutputStream().write(("Content-Length: " + length + "\r\n").getBytes("ASCII"));
        s.getOutputStream().write(blankline.getBytes("ASCII"));
        s.getOutputStream().write(query.getBytes("ASCII"));

        //Flush and wait for response...
        s.getOutputStream().flush();

然后我只需从服务器读取响应。

于 2013-01-29T23:35:02.137 回答
1

为什么不使用像 HttpClient 这样的库?它有一个非常好的 API 用于执行 HTTP GET、POST 和其他方法。它将允许您编写不那么脆弱且更易于理解的代码。

链接:http ://hc.apache.org/httpclient-3.x/

于 2013-01-29T22:45:36.050 回答