【问题标题】:Flex HTTPService does not include Content-Length header?Flex HTTPService 不包含 Content-Length 标头?
【发布时间】:2010-09-13 22:24:43
【问题描述】:

我正在尝试让 Flex 应用程序与我开发的自定义 python 网络服务器进行通信。

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

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

这是当前正在发送的标头:

主机:本地主机:7070 用户代理:Mozilla/5.0(Windows;U;Windows NT 5.1;en-US;rv:1.9.0.3)Gecko/2008092417 Firefox/3.0 .3 接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 接受语言:en-us,en;q=0.5 接受编码:gzip,deflate 接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7 保活:300 连接:保持活动

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

    我不认为这是一个已知问题。

    您确定没有发送 Content-Length 吗?您已经发布了来自浏览器的 HTTP 交互的请求端;协议的那一侧永远没有 Content-Length 标头。

    【讨论】:

      【解决方案2】:

      只要您将 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>
      

      还有这个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
      

      所以它应该可以工作。

      【讨论】:

        【解决方案3】:

        正如 Bill D 所说,您几乎可以肯定不会进行 POST,因为我们一直都在这样做,将它们与我们的服务器代码一起使用,并且它肯定包括 Content-Length。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-25
          • 2012-06-12
          • 2021-06-14
          相关资源
          最近更新 更多