Flex中如何打开网页及Get/Post数据 转
2024-07-13 00:07:16 221人阅读
flash中打开网页,以及用GET/POST二种方式向服务端发送数据
02 | btnOpen.addEventListener(MouseEvent.CLICK, |
04 | navigateToURL( new URLRequest( "http://www.g.cn/search?hl=zh-CN&q=" + encodeURIComponent (txtId.text)), "_blank" ); |
08 | btnSend.addEventListener(MouseEvent.CLICK, |
10 | sendToURL( new URLRequest( "/default.aspx?q=" + encodeURIComponent (txtId.text))); |
12 | btnPost.addEventListener(MouseEvent.CLICK,fnPostData); |
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(); |
21 | _urlReq.data = http://www.mamicode.com/_data; |
1 | protected void Page_Load( object sender, EventArgs e) |
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); |
如果发送了数据后,还要响应服务端的结果(比如取得服务端的返回值,再继续到Flash中处理),Flash中可这样写:
01 | var loader:URLLoader = new URLLoader(); |
02 | configureListeners(loader); |
03 | var request:URLRequest= new URLRequest( "/FlashHander.ashx?q=" + encodeURIComponent ( "菩提树下的杨过" )); |
06 | } catch (error:Error) { |
07 | trace ( "Unable to load requested document." ); |
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); |
21 | function completeHandler(event:Event): void { |
22 | var loader:URLLoader=URLLoader(event.target); |
23 | trace ( "completeHandler: " + loader.data); |
24 | lblReceive.text = loader.data; |
25 | var var s:URLVariables= new URLVariables(loader.data); |
26 | trace ( "The Method is " + var s.Method); |
30 | function openHandler(event:Event): void { |
31 | trace ( "openHandler: " + event); |
35 | function progressHandler(event:ProgressEvent): void { |
36 | trace ( "progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); |
40 | function securityErrorHandler(event:SecurityErrorEvent): void { |
41 | trace ( "securityErrorHandler: " + event); |
45 | function httpStatusHandler(event:HTTPStatusEvent): void { |
46 | trace ( "httpStatusHandler: " + event); |
50 | function ioErrorHandler(event:IOErrorEvent): void { |
51 | trace ( "ioErrorHandler: " + event); |
服务端FlashHander.ashx可以这样处理:
注意:返回的字符串格式为 name1=value1&name2=value2&name3=value3... 如果name和value中本身包含"="与"&",请注意用其它字符替换掉
02 | /// Summary description for FlashHander |
04 | public class FlashHander : IHttpHandler |
06 | public void ProcessRequest(HttpContext context) |
08 | context.Response.ContentType = "text/plain" ; |
09 | context.Response.Write( "msg=Hello World&Method=" + context.Request.HttpMethod + "&q=" + context.Request[ "q" ]); |
11 | public bool IsReusable |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。