【问题标题】:An HTTP POST request shows an HTML error instead of showing a the request bodyHTTP POST 请求显示 HTML 错误,而不是显示请求正文
【发布时间】:2014-12-08 15:07:49
【问题描述】:

我正在使用 casperjs 发布到 URL 并使用 fiddler2 来调试我的代码。下面是我的代码(写在coffeescript)。

casper = require('casper').create({
    waitTimeout    : 10000,
    verbose        : true,
    logLevel       : 'debug',
    userAgent      : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
    ignoreSslErrors: true,
    viewportSize   : {
        width : 1080,
        height: 1024
    }

})

url = "http://www.sample.com/test"
casper.start()
casper.thenOpen(url, {
        method: "post",
        data  : {
            a     : "aaa",
            b     : "bbb",
            c     : "ccc"
        },
        headers: {
            "User-Agent"  : "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36777",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "text/plain, */*"
        }
    },
    ->
        console.log this.getPageContent()
)

casper.run()

当我运行它时,我得到了以下调试信息:

C:\casperjs\batchbin\cj.bat C:\Users\***\WebstormProjects\haha\test.coffee
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://www.sample.com/test, HTTP POST
[debug] [phantom] Navigation requested: url=http://www.sample.com/test, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.sample.com/test"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://www.sample.com/test (HTTP 200)
<html xmlns="http://www.w3.org/1999/xhtml"><body><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Start tag expected.</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></body></html>
[info] [phantom] Step anonymous 2/2: done in 657ms.
[info] [phantom] Done 2 steps in 675ms

Process finished with exit code 0

但是,从提琴手,我的帖子请求是:

POST http://www.sample.com/test HTTP/1.1
Origin: null
Content-Length: ***
Accept: text/plain, */*
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36777
Cookie: ***
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en-US,*
Host: www.sample.com

a=aaa&b=bbb&c=ccc

我的回复是:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Date: Mon, 08 Dec 2014 14:36:34 GMT
Content-Length: ***

http://www.sample.com/this_is_a_sample_url

请注意,URL http://www.sample.com/this_is_a_sample_url 中的响应正文。但是getPageContent() 给了我一段 html 代码。起初我认为问题可能是由Accept 请求标头引起的。但是,它已经被设置为 text/plain 而不是 HTML。 谁能给我一些建议?

【问题讨论】:

    标签: post coffeescript phantomjs casperjs


    【解决方案1】:

    您的服务器的响应可能包含正确的数据,但由于它以 XML 形式返回 URL,PhantomJS 无法正确解析它。这就是为什么显示为 HTML 页面的错误。

    您应该专门使用__utils__.sendAJAX 来下载内容(在 JavaScript 中):

    casper.post = function(url, data){
        return this.evaluate(function(targetURL){
            return __utils__.sendAJAX(targetURL, "POST", data, false, {
                overrideMimeType: "text/plain"
            });
        }, url);
    };
    
    casper.start("http://example.com").then(function(){
        var content = this.post(targetURL, {
            a     : "aaa",
            b     : "bbb",
            c     : "ccc"
        });
        // do something with content
    }).run();
    

    在开始时会打开一个虚拟(现有)页面以正确初始化系统。当虚拟域与您的实际请求域不同时,您还需要使用 --web-security=false 命令行选项运行 CasperJS。

    如果您在 start 上使用虚拟本地 html 文件,则应添加 --local-to-remote-url-access=true 命令行选项,因为当前 URL 为 about:blank 并且 AJAX 请求仅限于当前域。

    【讨论】:

    • 嗨 Artjom,我应该把这段代码放在哪里?我把它放在顶层并得到“CasperError:没有定义步骤,正在中止”。还有一种方法可以指定自定义 HTTP 请求标头吗?我只需要发布到一个 URL,没有其他步骤,所以我认为我不能在这个 URL 上start
    • then 块内,我也忘记了必要的命令行选项。请参阅我的更新答案。
    • 当我得到返回值时,我使用你提供的代码和console.log(content)。我看不到提琴手的请求。我错过了什么?我得到的是:[info] [phantom] 开始... [info] [phantom] 运行套件:1 步 [debug] [phantom] 成功注入 Casper 客户端实用程序 null [info] [phantom] 步骤匿名 1/ 1:在 28 毫秒内完成。 [info] [phantom] 在 44 毫秒内完成 1 步进程以退出代码 0 结束
    • 刚刚试了一下。它应该起作用了。我找到了解决方法。
    • 我也对其进行了一些更改,以便它现在可以重复使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多