2008-09-29 36 views
0

我想獲得一個Flex應用程序來與我開發的自定義python webserver進行通信。Flex HTTPService不包含Content-Length標頭?

我注意到我無法讀取收到的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 

回答

0

我不相信這是一個已知的問題。

您確定沒有發送Content-Length?您已經發布了來自瀏覽器的HTTP交互的請求端;在協議的這一側從來沒有一個Content-Length頭。

4

只要您將HTTPService的方法屬性設置爲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> 

而且這條巨蟒服務器代碼:

#!/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 

所以它應該工作。

0

正如Bill D所說,你幾乎可以肯定不會做POST,因爲我們一直在做這些事情,並且使用我們的服務器代碼來實現它們,而且肯定會包含Content-Length。