Flash新手求助:网页中SWF发起PUT请求失败排查
Hey there! As someone who’s wrestled with Flash’s network quirks before, let’s break down why your PUT request might be failing—even though Adobe’s docs confirm the method is supported. Here are the most common pitfalls to check:
1. Flash Security Sandbox & Cross-Domain Restrictions
Flash has notoriously strict sandbox rules that often trip up new developers. If your SWF is hosted on domainA.com and you’re sending a PUT request to domainB.com, the target server must have a valid crossdomain.xml file in its root directory that explicitly allows PUT requests from your SWF’s domain.
A minimal working example of this file would look like:
<?xml version="1.0"?> <cross-domain-policy> <!-- Allow your SWF's domain to access this server --> <allow-access-from domain="your-swf-domain.com" /> <!-- Allow custom headers if you're using them --> <allow-http-request-headers-from domain="your-swf-domain.com" headers="*" /> <!-- Critical: Explicitly whitelist the PUT method --> <allow-methods-from domain="your-swf-domain.com" methods="PUT" /> </cross-domain-policy>
If you’re testing locally (opening a local SWF file), you’ll also need to adjust Flash Player’s global security settings to allow local files to access network resources.
2. Incorrect URLRequestMethod Usage
Double-check how you’re setting the request method in your PoC. The standard, reliable approach is to use the URLRequestMethod.PUT enum—not just a raw string "PUT" (some versions might tolerate the string, but it can lead to unexpected behavior):
var request:URLRequest = new URLRequest("your-target-url"); // Correct usage request.method = URLRequestMethod.PUT; // Avoid this: request.method = "PUT";
3. Malformed Request Data or Content-Type Mismatch
PUT requests require properly formatted data and a matching contentType header. For example:
- If sending form data, set
contentTypeto"application/x-www-form-urlencoded"and encode your data withURLEncoder. - If sending JSON, use
"application/json"and encode your object to a string before converting it to a ByteArray.
Here’s a quick example of encoding form data correctly:
var data:String = "key1=value1&key2=value2"; request.data = new ByteArray(); request.data.writeUTFBytes(URLEncoder.encode(data)); request.contentType = "application/x-www-form-urlencoded";
4. Missing Error & Status Event Listeners
Flash won’t surface network errors by default—you need to explicitly listen for IOErrorEvent.IO_ERROR and HTTPStatusEvent.HTTP_STATUS to see what’s actually going wrong. Add these listeners to your URLLoader:
var loader:URLLoader = new URLLoader(); loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError); loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatus); loader.load(request); function onIOError(e:IOErrorEvent):void { trace("IO Error: " + e.text); // This will reveal cross-domain blocks, connection issues, etc. } function onHttpStatus(e:HTTPStatusEvent):void { trace("HTTP Status Code: " + e.status); // Look for codes like 403 (forbidden), 404 (not found), or 500 (server error) }
5. Server-Side PUT Support
Don’t overlook the server side! Even if Flash supports PUT, your server might not be configured to handle it:
- Apache needs
AllowMethods PUTin its directory configuration. - Nginx requires proper location block settings to allow PUT requests.
- Backend frameworks (like PHP) don’t automatically parse PUT data—you’ll need to read from
php://inputmanually.
Next Steps
Start by adding the error listeners to get specific feedback. Then verify the cross-domain policy file exists and is correctly configured. If those check out, double-check your request method and data encoding.
内容的提问来源于stack exchange,提问作者CaptainRR

