Interaction of AS and asp.net--detailed urlrequest

Source: Internet
Author: User
Tags object json net variables return

In. NET we know that there is a thing called webhttprequest, with which we can achieve a variety of network snooping, monitoring, acquisition and robotics, if an additional

A pattern recognition, that's really hanging up ...

In as we can also achieve the same function, and we can cooperate with JS to do cross-domain, collect cookies and so some user behavior analysis, is also a web development

A sharp weapon.

One: Building communication with external programs

  

1: Generally take three steps

<1> create the Request object using URLRequest.

<2> load the Request object with Urlloader and then make the request.

<3> listens to Urlloader objects, and feedback the results of the processing through various events.

II: Building URLRequest

Here's a look at the common properties in URLRequest

1:url

This is very simple, the request URL, can be absolute or can be relative.

2:method

As Ajax knows, it is used to control whether a form is submitted by post or get, what get cannot be greater than 2k,post unrestricted, and so on.

3:requestheader

We know that a mock commit will append various parameters to the HTTP head to deceive the server, pretending to be a real human request.

4:data

That is, the data that comes with the server at the time of the request, in JSON format, the kv form of the URL can be, but must be encapsulated with Urlvariables.

5: Simple Demo

var url= "http://localhost:25212/index.aspx";

var vari:urlvariables=new urlvariables ();

Vari.data= "{\" id\ ": \" 2\ "}";

var req:urlrequest=new urlrequest (URL);

Req.data=vari;

Req.method=urlrequestmethod.post;

Three: Building Urlloader objects

<: In as, all urlrequest need to be loaded with urlloader to interact with back-end programs, and 5 events are provided to monitor the status of the current request.

1:open Events

Events executed at the start of the request operation

2:progress Events

Commonly used to monitor download progress, you can view "loaded data" and "total data" in real time through byteloaded and bytetotal.

3:complete Events

The events that are invoked after the data has been successfully loaded are also most commonly used.

4:ioerror Events

Like Ajax, an event that is invoked when a request fails.

< two: When the data is returned remotely, the data will be stored in the Urlloader in the Data property, of course, the type of return, depending on our post

The Urlloaderdataformat of the time specified.

1:urlloaderdataformat.text

Specifies that the current return data value must be a text value, which is also the default value.

2:urlloaderdataformat.binary

Sometimes we need to specify the binary attribute when flash needs to load the binary data on the web side.

3:urlloaderdataformat.variables

This is to specify that the contained data is a URL-coded data, so we use variables to decode it.

Using Urlloader to load
var loader:urlloader=new urlloader (req);

Loader.addeventlistener (event.complete,oncomplete);
Loader.addeventlistener (ioerrorevent.io_error,onerror);

Request Load Completion
function OnComplete (e:event): void{
Trace (loader.data);
}

In general, let's do an experiment, and pass JSON to the server to be processed by the server side.

1: Server-side code, just return the request parameter as-is

public partial class Index:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)           
{
var data = request.form["Data"];

Response.Write ("Congratulations, Data Request Success ~" + date);
}

2:as Code

Import Flash.net.URLLoader;
Import Flash.net.URLRequest;
Import flash.events.Event;
Import Flash.text.TextField;
Import Flash.utils.Timer;
Import flash.events.TimerEvent;

var url= "http://localhost:25212/index.aspx";

var vari:urlvariables=new urlvariables ();

Vari.data= "{\" id\ ": \" 2\ "}";

var req:urlrequest=new urlrequest (URL);

Req.data=vari;

Req.method=urlrequestmethod.post;

Using Urlloader to load
var loader:urlloader=new urlloader (req);

Loader.addeventlistener (event.complete,oncomplete);
Loader.addeventlistener (ioerrorevent.io_error,onerror);

Request Load Completion
function OnComplete (e:event): void{
Trace (loader.data);
}

IO Request error
function OnError (e:ioerrorevent): void{
Trace ("Sorry, data request Error");
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.