将POST请求标头发送到Web服务器

时间:2013-01-29 22:25:29

标签: java http http-post httpclient undefined-index

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分: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();

然后我只是从服务器上读回来。