ActionScript與Web程式通訊

來源:互聯網
上載者:User

著作權聲明:轉載時請以超連結形式標明文章原始出處和作者資訊及本聲明
http://ejb3.blogbus.com/logs/33647479.html

Actionscript與Web程式通訊,跟瀏覽器和伺服器的原理一樣。Flash本身可以運行在瀏覽器中,所以可以把它當做瀏覽器用戶端看待,遵循的是HTTP協議。這裡的Web程式可以是JSP、PHP、ASP等。

在HTTP協議中,用戶端發送一個request,伺服器端接受request處理後返回一個response。在ActionScript 就是通過這種原理。在AS的flash.net包裡主要由URLRequest,URLLoader,URLVariables這三個類負責處理通訊任務。URLRequest類相當於request,URLLoader相當於response,只不過這裡的request是由用戶端發出,response由用戶端接收,與伺服器端相反,形成互連。而URLVariables則是作為發送資料的參數對象。

Actionscrip代碼:

package {
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
   
    public class Main extends Sprite {
       
        private var request:URLRequest;
       
        private var loader:URLLoader;
       
        private var variable:URLVariables;
       
        public function Main() {
            request = new URLRequest("http://localhost:8080/flash/login");
            variable = new URLVariables();
            variable.username = "zhang";
            request.method = URLRequestMethod.GET;
            request.data = variable;
            loader = new URLLoader();
            loader.load(request);
        }
    }
}

以上代碼只是向伺服器發送一個request,並沒有捕獲服務端返回來的response;如要捕獲,還需要對loader添加一個監聽器

public function Main() {
            request = new URLRequest("http://localhost:8080/flash/login");
            variable = new URLVariables();
            variable.username = "zhang";
            request.method = URLRequestMethod.GET;
            request.data = variable;
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE,onLoad)
            loader.load(request);
        }
       
        private function onLoad(e:Event):void {
            trace(loader.data);
        }

伺服器端:

public class Login extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
    public Login() {
        super();
    }      
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }     
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        System.out.println(username);
        PrintWriter out = response.getWriter();
        out.print("your username is"+username);
        out.flush();
        out.close();
    }                
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.