我试图在Adobe Flex中创建一个简单的HTTP URLReqest,大致是代码:
var requestSender:URLLoader = new URLLoader();
var urlRequest :URLRequest = new URLRequest("http://localhost:8888");
var msg:String = "data=blah";
urlRequest.data = msg;
urlRequest.contentType = "application/x-www-form-urlencoded";
urlRequest.method = URLRequestMethod.POST;
这会产生以下内容:
POST / HTTP/1.1
Referer: app:/PersonSearch.swf
Accept: text/xml, application/xml, application/xhtml+xml, ...
x-flash-version: 10,1,85,3
Content-Type: application/x-www-form-urlencoded
Content-Length: 102
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (Windows; U; en-US) ...
Host: 127.0.0.1:8888
Connection: Keep-Alive
data=blah
我真正想要的是:
POST / HTTP/1.1
Content-Type:application/x-www-form-urlencoded
Connection:close
Via:MDS_777
Accept:*/ *
Host:localhost:8888
Content-Length:104
data=blah
任何人都知道如何删除 Accept-Encoding 等字段,添加“Via”等字段并将连接设置为“关闭”?
另外我们如何从HTTP请求中获得响应?
由于 菲尔
答案 0 :(得分:1)
Flash Player不允许您通过ActionScript更改 Accept-Encoding 或 Via 标头。如果您尝试这样做,您将收到类似这样的消息:
错误#2096:HTTP请求标头 无法通过设置Accept-Encoding 动作脚本。
如果您使用的是URL变量,可以尝试通过以下方式简化代码:
var variables:URLVariables = new URLVariables();
variables.data = "blah"; //same as doing data=blah
variables.data2 = "blah2"; //same as doing data2=blah2
var requestSender:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest("http://localhost:8888");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = variables;
要获得回复,您必须在“requestSender”上收听Event.COMPLETE
:
requestSender.addEventListener(Event.COMPLETE, completeHandler);
private function completeHandler(event:Event):void {
// do something with requestSender.data (or URLLoader(event.target).data)
}