Flex HTTPService是否不包含Content-Length标头?
时间:2020-03-06 14:51:37 来源:igfitidea点击:
我正在尝试让Flex应用程序与我开发的自定义python网络服务器进行通信。
我注意到我无法读取收到的数据,因为Flex似乎没有在HTTP标头中包含Content-Length。 (从纯HTML发布到Web服务器时,我的网络服务器可以正常工作)
这是一个已知问题吗?任何想法如何设置content-length标头?
这是当前正在发送的标头:
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
解决方案
我不认为这是一个已知问题。
我们确定没有发送"内容长度"吗?我们已经从浏览器中发布了HTTP交互的请求端;协议的那一侧永远不会有Content-Length标头。
只要我们将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
因此,它应该工作。
正如Bill D所说,我们几乎可以肯定没有在做POST,就像我们一直在做的那样,将它们与我们的服务器代码一起提供给他们,并且肯定包括Content-Length。