Moscrif API Docs for: 2012q3
Show:

WebClient Class

Library: net

This class allows to perform web methods.

Example:

const SERVER = "www.example.com";
const PORT = 80;    // for http protocol

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
this.web.onError = function(sender)
{
    System.messageBox("An error occurred!");
}

this.web.onReceiving = function(sender, received, total)
{
    console<<"downloaded " + received + "B.\n";
}

this.web.onReceived = function(sender)
{
    // to decode text use:
    System.messageBox("received: " + this.data.toString("utf8"));
    // to decode image file use:
    this super.img = Bitmap.fromBytes(this.data);
    app.invalidate();

}

this.web.onCancel = function(sender)
{
    System.messageBox("Downloading canceled!");
}

this.web.getData("/getdata/anyscript");

Methods

this

(
  • file
)
WebClient

Object constructor.

Parameters:

  • file String

    Name of archive to create.

Returns:

WebClient: New instance of Zip class, which manages operations with archive

Example:

    this.web = new WebClient();
    this.web.open(SERVER, PORT, false, "");
    this.web.onReceived = function(sender)
    {
           ...
    }
    this.web.getData("/getdata/anyscript");

cancel

()

This function cancel running operations of WebClient.

getData

(
  • resource
  • [responseToFile]
)

Easy way to get data from server. Data from the server are saved to a file to location set by second parameter.

Parameters:

  • resource String

    Server resource file.

  • [responseToFile] String optional

    Destination location.

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
this.web.getData("/getdata/anyscript");

open

(
  • host
  • port=80
  • useSSL=false
  • [proxy]
)

Function opens web location for working.

Parameters:

  • host String

    Url of required location.

  • port=80 Integer

    Port for conection, the default isNaN 80.

  • useSSL=false Boolean

    Specifies if the web object uses SSL or not, the default is false.

  • [proxy] String optional

    Determines the proxy server, the default is none.

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");

postData

(
  • resource
  • data
  • [responseToFile]
)

Function posts data to the server.

Parameters:

  • resource String

    erver resource location

  • data Bytes | String

    Data to send. Byte of array or text string

  • [responseToFile] String optional

    Location of file, where response would be saved.

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
web.postData("/import.php", data);

postFile

(
  • resource
  • fileToPost
  • [responseToFile]
)

Function posts data to the server.

Parameters:

  • resource String

    erver resource location

  • fileToPost String

    Path to the file to send.

  • [responseToFile] String optional

    Location of file, where response would be saved.

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
web.postData("/import.php", data);

Properties

data

Bytes

Received vector of bytes.

errorCode

Integer

Last code error.

errorMessage

String

Last error message generated by the WebClient object.

host

String

Server URL.

port

Integer

Port number.

secure

Boolean

Value is true if connection is secured by SSL protocol. Otherwise the value is false.

state

Integer

Number representatin of state.

Events

onCancel

Callback which is called when some web operation is cancelled.

Example:

    this.web = new WebClient();
    this.web.open(SERVER, PORT, false, "");
    this.web.onCancel = function(sender)
    {
            System.messageBox("Downloading canceled!");
    }

    this.web.getData("/getdata/anyscript");

onError

Callback which is called when an error appears.

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
this.web.onError = function(sender)
{
        System.messageBox("An error occurred!");
}
this.web.getData("/getdata/anyscript");

onReceived

Callback function which is called when new data has been received.

Example:

 this.web = new WebClient();
 this.web.open(SERVER, PORT, false, "");
 this.web.onReceived = function(sender)
 {
        // to decode text use:
        System.messageBox("received: " + this.data.toString("utf8"));
        // to decode image file use:
        this super.img = Bitmap.fromBytes(this.data);
        app.invalidate();
 }
this.web.getData("/getdata/anyscript");

onReceiving

Callback function which is called while receiving new data.

Event Payload:

  • bytes Integer

    Number of received bytes

  • bytesTotal Integer

    Number of all bytes to receive, can be zero (depends on server).

Example:

this.web = new WebClient();
this.web.open(SERVER, PORT, false, "");
this.web.onReceiving = function(sender, received, total)
{
    console<<"downloaded " + received + "B.\n";
}
this.web.getData("/getdata/anyscript");

onSending

Callback function which is called while sending data.

Event Payload:

  • bytes Integer

    Number of bytes sent to data.

  • bytesTotal Integer

    Total bytes to be sent to server.

Example:

    his.web = new WebClient();
    this.web.open(SERVER, PORT, false, "");
     this.web.onReceiving = function(sender, received, total)
     {
            console<<"downloaded " + received + "B.\n";
     }
    this.web.getData("/getdata/anyscript");