flash中打开网页,以及用GET/POST二种方式向服务端发送数据
01 |
//按下按钮,打开网页 |
02 |
btnOpen.addEventListener(MouseEvent.CLICK, |
03 |
function(){ |
04 |
navigateToURL(new URLRequest("http://www.g.cn/search?hl=zh-CN&q=" + encodeURIComponent(txtId.text)),"_blank"); |
05 |
}); |
06 |
07 |
//以Get方式发送数据(发送就完事,不会理会服务端是否响应) |
08 |
btnSend.addEventListener(MouseEvent.CLICK, |
09 |
function(){ |
10 |
sendToURL(new URLRequest("/default.aspx?q=" + encodeURIComponent(txtId.text))); |
11 |
}); |
12 |
btnPost.addEventListener(MouseEvent.CLICK,fnPostData); |
13 |
14 |
//以Post方式发送数据(同样:发送就完事,不会理会服务端是否响应) |
15 |
function fnPostData(e:MouseEvent) { |
16 |
var _urlReq:URLRequest = new URLRequest(); |
17 |
_urlReq.url = "/default.aspx"; |
18 |
_urlReq.method = URLRequestMethod.POST; |
19 |
var _data:URLVariables = new URLVariables(); |
20 |
_data.q = "菩提树下的杨过"; //即传递 q = 菩提树下的杨过,注:经测试,Flash会自动对传递的数据做encodeURIComponent处理,所以此外不能再加encodeURIComponent,否则就是二次编码了 |
21 |
_urlReq.data = _data; |
22 |
sendToURL(_urlReq); |
23 |
} |
1 |
protected void Page_Load(object sender, EventArgs e) |
2 |
{ |
3 |
string q = Request["q"]; |
4 |
if (!string.IsNullOrEmpty(q)) { |
5 |
string _file = Server.MapPath("~/log.txt"); |
6 |
File.AppendAllText(_file,q + "\t" + Request.HttpMethod + "\t" + DateTime.Now + Environment.NewLine); |
7 |
} |
8 |
} |
如果发送了数据后,还要响应服务端的结果(比如取得服务端的返回值,再继续到Flash中处理),Flash中可这样写:
01 |
var loader:URLLoader = new URLLoader(); |
02 |
configureListeners(loader); |
03 |
var request:URLRequest=new URLRequest("/FlashHander.ashx?q=" + encodeURIComponent("菩提树下的杨过")); |
04 |
try { |
05 |
loader.load(request); |
06 |
} catch (error:Error) { |
07 |
trace("Unable to load requested document."); |
08 |
} |
09 |
10 |
//定义各种情况的回调函数 |
11 |
function configureListeners(dispatcher:IEventDispatcher):void { |
12 |
dispatcher.addEventListener(Event.COMPLETE, completeHandler); |
13 |
dispatcher.addEventListener(Event.OPEN, openHandler); |
14 |
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); |
15 |
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); |
16 |
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); |
17 |
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); |
18 |
} |
19 |
20 |
//加载完成时,将触发 |
21 |
function completeHandler(event:Event):void { |
22 |
var loader:URLLoader=URLLoader(event.target); |
23 |
trace("completeHandler: " + loader.data); |
24 |
lblReceive.text = loader.data; //本例中,服务端返回: msg=Hello World&Method=GET&q=菩提树下的杨过 |
25 |
var vars:URLVariables=new URLVariables(loader.data); |
26 |
trace("The Method is " + vars.Method); //服务端返回的字符串中如果有 Method=xxx 这样的字符,则Flash中可以直接用vars.Method进行访问 |
27 |
} |
28 |
29 |
//刚开始请求时,将触发 |
30 |
function openHandler(event:Event):void { |
31 |
trace("openHandler: " + event); |
32 |
} |
33 |
34 |
//下载进度发生变化时,将触发(可利用这个做加载进度条) |
35 |
function progressHandler(event:ProgressEvent):void { |
36 |
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); |
37 |
} |
38 |
39 |
//因安全原因出现错误时,将触发 |
40 |
function securityErrorHandler(event:SecurityErrorEvent):void { |
41 |
trace("securityErrorHandler: " + event); |
42 |
} |
43 |
44 |
//http请求状态变化时,将触发 |
45 |
function httpStatusHandler(event:HTTPStatusEvent):void { |
46 |
trace("httpStatusHandler: " + event); |
47 |
} |
48 |
49 |
//io错误时,将触发 |
50 |
function ioErrorHandler(event:IOErrorEvent):void { |
51 |
trace("ioErrorHandler: " + event); |
52 |
} |
服务端FlashHander.ashx可以这样处理:
注意:返回的字符串格式为 name1=value1&name2=value2&name3=value3... 如果name和value中本身包含"="与"&",请注意用其它字符替换掉
01 |
/// <summary> |
02 |
/// Summary description for FlashHander |
03 |
/// </summary> |
04 |
public class FlashHander : IHttpHandler |
05 |
{ |
06 |
public void ProcessRequest(HttpContext context) |
07 |
{ |
08 |
context.Response.ContentType = "text/plain"; |
09 |
context.Response.Write("msg=Hello World&Method=" + context.Request.HttpMethod + "&q=" + context.Request["q"]); |
10 |
} |
11 |
public bool IsReusable |
12 |
{ |
13 |
get |
14 |
{ |
15 |
return false; |
16 |
} |
17 |
} |
18 |
} |
Flex中如何打开网页及Get/Post数据 转,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/regalys168/p/3855962.html