Flex HTTPService不包含Content-Length标头?

时间:2008-09-29 04:26:06

标签: flex actionscript-3

我正在尝试使用Flex应用程序与我开发的自定义python webserver进行通信。

我注意到我无法读取收到的postdata,因为Flex似乎没有在HTTP标头中包含Content-Length。 (我的网络服务器在从纯HTML发布时工作)

这是一个已知问题吗?任何想法如何设置内容长度标题?

以下是发送的当前标头:

Host: localhost:7070

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0

.3

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

3 个答案:

答案 0 :(得分:4)

只要您将HTTPService的method属性设置为POST,就应该这样做。如果省略它,它将默认为GET,参数将作为查询字符串的一部分发送,而不是POST数据。

我使用此Flex代码设置了此方案:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application  layout="absolute"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="init()">

    <mx:HTTPService id="service" 
        url="http://localhost:8000/"
        method="POST"
        resultFormat="text"
        result="response.htmlText=ResultEvent(event).result.toString()"/>

    <mx:Text id="response" width="100%" height="100%"/>

    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;
            private function init() : void {
                service.send({
                    foo: "Fred",
                    bar: "Barney"
                });
            }
        ]]>
    </mx:Script>
</mx:Application>

这个python服务器代码:

#!/usr/bin/env python

import SimpleHTTPServer, BaseHTTPServer, string

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("<html><body>")
        self.wfile.write("<b>METHOD:</b> " + self.command)

        # Write out Headers
        header_keys = self.headers.dict.keys()
        for key in header_keys:
            self.wfile.write("<br><b>" + key + "</b>: ")
            self.wfile.write(self.headers.dict[key])

        # Write out any POST data
        if self.headers.dict.has_key("content-length"):
            content_length = string.atoi(self.headers.dict["content-length"])
            raw_post_data = self.rfile.read(content_length) 
            self.wfile.write("<br><b>Post Data:</b> " + raw_post_data) 
        self.wfile.write("</body></html>")
    def do_GET(self):
        self.do_POST()

try:
    BaseHTTPServer.test(MyHandler, BaseHTTPServer.HTTPServer)
except KeyboardInterrupt:
    print 'Exiting...'

得到了这个结果:

METHOD: POST
content-length: 19
accept-language: en-us,en;q=0.5
accept-encoding: gzip,deflate
connection: keep-alive
keep-alive: 300
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
host: 10.0.7.61:8000
content-type: application/x-www-form-urlencoded
Post Data: bar=Barney&foo=Fred

所以它应该有用。

答案 1 :(得分:0)

我不相信这是一个已知的问题。

您确定没有发送内容长度吗?您已经发布了来自浏览器的HTTP交互的请求方面;协议的那一边永远不会有Content-Length标头。

答案 2 :(得分:0)

正如Bill D所说,你几乎肯定没有做POST,因为我们一直在做这些,用我们的服务器代码来处理它们,它肯定包括Content-Length。