Chinaunix

标题: 一个超级简单的lwp问题! [打印本页]

作者: lvlfforever    时间: 2007-02-01 13:58
标题: 一个超级简单的lwp问题!
我作了一个测试文件 test.php
<?php
    if($_POST['submit'])
    {
        $user = trim($_POST['username']);
        if(!empty($user))
        {
                echo "Right!";
                exit;

        }else{
                echo "Wrong";
                exit;
        }
    }


?>



<html>
<title>test </title>
<meta http-equiv="content-type" content="text/html; charset=GB2312">
<body>
<center>
<form method=POST action=/test.php>
<table>
<tr>
    <td>姓名:</td><td><input type=text name=username></td>
</tr>
</table>
<input type=submit name=submit value=" 提交  ">
</form>
</center>
</body>
</html>


之后在本机用写了一个perl文件(test.pl),模拟提交的,代码如下:

#!/usr/bin/perl

use strict;
use LWP;
use URI::Escape;
use HTTP::Request::Common;

use constant RFC_SEARCH  => 'http://10.3.1.107/test.php';
use constant RFC_REFERER => 'http://10.3.1.107/test.php';
my $ua       = LWP::UserAgent->new;
my $search_terms = "@ARGV";
my $request = POST ( RFC_SEARCH,
                     Content => [ username   => $search_terms ],
                     Referer => RFC_REFERER
                   );

my $response = $ua->request($request);
die $response->message unless $response->is_success;
my $content = $response->content;
print $content;


我运行 test.pl ddd
问题是,打印出来的$content之后,既不是Right,也不是Wrong,而是HTML源代码。请问什么原因???
十分感谢~~
作者: ulmer    时间: 2007-02-01 16:36
标题: 回复 1楼 lvlfforever 的帖子
simplest way to set your php script httpd header as "content-type: text/plain"
after $_POST['submit'] was checked.
for eaxample:
<?php
if ($_POST['submit']) {
    header("content-type: text/plain");
    ....
}
?>


PHP as default outputs usually "content-type: text/html" with some html z´tag.

-- ulmer
作者: Namelessxp    时间: 2007-02-01 16:56
use constant RFC_SEARCH  => 'http://10.3.1.107/test.php?submit=1';

or
my $request = POST ( RFC_SEARCH,
                     Content => [ username   => $search_terms ,submit => 1],
                     Referer => RFC_REFERER
                   );
作者: lvlfforever    时间: 2007-02-02 11:38
多谢二位,光明使者的方法解决了问题。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2