转载于:http://blog.sina.com.cn/s/blog_94b9dc0c0100x2pw.html
服务 tomcat 6.0(应该是)
1. test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
// - - 如果你做过java应该知道这是什么
request.setCharacterEncoding("utf-8");
out.clear();
// 返给前台的数据
out.print("success1=111");
// 如果是2个的话要用&隔开(URL传值)
out.print("&success2=222");
%>
2.HttpTest.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.text.TextField;
import flash.text.TextFieldType;
public class HttpTest extends Sprite
{
//服务端的http地址字符串
private var urlStr:String = "http://127.0.0.1:8080/web/test.jsp";
public function HttpTest()
{
reuqestServer();
}
//请求服务端
private function reuqestServer():void
{
//创建URLLoader对象
var urlLoader:URLLoader = new URLLoader();
//设置接收数据方式(文本、原始二进制数据、URL 编码变量);
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
//设置传递参数
var urlVars:URLVariables = new URLVariables();
urlVars.name = "go333333333333";
//urlVars.password="1111";
//建立Request访问对象
var urlRequest:URLRequest = new URLRequest(urlStr);
//设置参数
urlRequest.data = urlVars;
//设置访问模式(POST,GET)
urlRequest.method = URLRequestMethod.POST;
try
{
trace(urlRequest.data);
trace(urlRequest.url);
// 使用 load 可以发送url请求
urlLoader.load(urlRequest);
}
catch(error:Error)
{
trace(error)
}
//加载完成
urlLoader.addEventListener(Event.COMPLETE, urlLoaderCompleteHandler);
//开始访问
urlLoader.addEventListener(Event.OPEN, urlLoaderOpenHandler);
//加载进度
urlLoader.addEventListener(ProgressEvent.PROGRESS, urlLoaderProgressHandler);
//跨域访问安全策略
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, urlLoaderSecurityErrorHandler);
//Http状态事件
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, urlLoaderHttpStatusHandler);
//访问出错事件
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, urlLoaderIoErrorHandler);
}
private function urlLoaderCompleteHandler(e:Event):void
{
var txt:TextField = new TextField();
txt.wordWrap = true;
txt.width = 300;
txt.type = TextFieldType.DYNAMIC;
txt.text = "数据请求完成,返回数据为:" + e.target.data.success1;
txt.text += "数据请求完成,返回数据为:" + e.target.data.success2;
txt.y = 40;
txt.multiline = true;
this.addChild(txt);
}
private function urlLoaderOpenHandler(e:Event):void
{
var txt:TextField = new TextField();
txt.text = "连接已经打开";
txt.multiline = true;
txt.y = 20;
this.addChild(txt);
}
private function urlLoaderProgressHandler(e:ProgressEvent):void
{
var num:uint = (e.bytesLoaded / e.bytesTotal) * 100;
var txt:TextField = new TextField();
txt.text = num + "%";
txt.y = 0;
this.addChild(txt);
}
private function urlLoaderSecurityErrorHandler(e:SecurityErrorEvent):void
{
trace(e);
}
private function urlLoaderHttpStatusHandler(e:HTTPStatusEvent):void
{
trace(e);
}
private function urlLoaderIoErrorHandler(e:IOErrorEvent):void
{
trace(e);
}
}
}